Android – Setting up Push Notifications

  1. Get your GCM credentials

 

Go to the GCM guide on how to setup an Android device, scroll down and click on the “GET A CONFIGURATION FILE” button. Follow the steps to create/choose a project and enable Google Cloud Messaging. You’ll be given a Server API Key and a Sender ID. Take note of these two values, as they are your GCM Android credentials.

 

  • Registration ID: The GCM registration ID uniquely identifies an app/device pairing for push purposes.
  • Sender ID: The GCM sender ID is a public number that identifies the sender of a push notification.
  • API key: The GCM API key is a server secret that allows a server to send pushes to a registration ID on behalf of a particular sender ID.

 

 

  1. Configure your app on the back4app dashboard

 

Click on your app on the back4app dashboard and choose “Android Push Notification Settings”.

Captura de Tela 2016-04-27 às 16.25.50

On the settings screen add the API Key and Sender ID you got on the last step.

Captura de Tela 2016-04-27 às 16.26.28

 

 

  1. Edit your AndroidManifest.xml

 

Your AndroidManifest.xml should look something like the following. Be careful to not just override yours, since it may have specific information. Parts to be aware of are:

  • the user permissions required by the push service;
  • replacing YOUR_SENDER_ID on the “com.parse.push.gcm_sender_id” meta-data with the Sender ID you got on the first step;
  • the service and broadcast receiver definitions before the application closing tag;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.directions.sample">

   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.WAKE_LOCK" />
   <uses-permission android:name="android.permission.VIBRATE" />
   <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
   <permission android:protectionLevel="signature" 
    android:name="com.directions.sample.permission.C2D_MESSAGE" />
   <uses-permission android:name="com.directions.sample.permission.C2D_MESSAGE" />
   <!-- Other permissions for your app... -->

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme">
       <activity android:name=".Main">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

       <!-- Parse setup -->
       <meta-data
           android:name="com.parse.APPLICATION_ID"
           android:value="YOUR_APPLICATION_ID" />
       <meta-data
           android:name="com.parse.CLIENT_KEY"
            android:value="YOUR_CLIENT_KEY" />
       <!-- GCM setup -->
       <meta-data
           android:name="com.parse.push.gcm_sender_id"
           android:value="id:YOUR_SENDER_ID" />
       <!-- The following service and broadcast receiver definitions must come immediately 
            before the closing </application> tag -->
       <service android:name="com.parse.PushService" />
       <receiver android:name="com.parse.ParsePushBroadcastReceiver"
           android:exported="false">
            <intent-filter>
               <action android:name="com.parse.push.intent.RECEIVE" />
               <action android:name="com.parse.push.intent.DELETE" />
               <action android:name="com.parse.push.intent.OPEN" />
           </intent-filter>
       </receiver>
       <receiver android:name="com.parse.GcmBroadcastReceiver"
           android:permission="com.google.android.c2dm.permission.SEND">
           <intent-filter>
               <action android:name="com.google.android.c2dm.intent.RECEIVE" />
               <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
               <category android:name="com.directions.sample" />
           </intent-filter>
       </receiver>
   </application>

</manifest>

  1. Update your Installation objects

 

In order for an app (Installation) to receive push notifications using a custom GCM Sender ID, the respective Installation object should have the “GCMSenderId” field set to match the Sender ID you got on the first step. You can do that manually, opening the Browser on the app’s Parse Dashboard and inserting the Sender ID by hand on the right column. If you would rather have that value set when the user opens the app, you could put a code like the following on your main activity:

 

// Parse setup
Parse.initialize(this);

// Update Installation
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "YOUR_SENDER_ID");
installation.saveInBackground();

 

Again, remember to replace YOUR_SENDER_ID with the value you got on the first step.

 

 

  1. Create a new build and test

 

Please, follow these steps to generate a new app version and test to see if the app is able to receive the notification. If there are any issues still, just let us know =)