Skip to content

Android Push Notification Configuration

This quick guide will take you through the steps required to configure your In-app Referral push notifications


Android Push Notifications serve as the holy grail for customer engagement & activation in the current state of affairs.

Here you shall understand the nuances of configuring the same for your app, in conjunction with using it for triggering AppVirality backed Rewards’ Notification in a synchronous and automated manner.

STEP 1 Enable GCM


Enabling Google Cloud Messaging in your Google API Console

Create Project - GCM

Create Project - GCM

To start with, turn on the Google Cloud Messaging Services from Google’s API Console Page

Now, click on Create Project to create new project.

Once you have created a new project (or if you already have an existing project), you will be taken to the console’s dashboard.

Note the project number (top pane of the page)

This 12 digit number will be your Google Sender ID, which you will need to use in your code to register your application for push notifications.

Project Number - GCM

Project Number - GCM

From the Google Developer Console page, select the APIs section under “APIs & auth” from the left-hand navigation.

API & Authentication - GCM

API - GCM

Find Google Cloud Messaging for Android in the list of services under APIs & auth, and turn it ON by clicking the switch at the top of the page, as shown below.

Enable API

Enable API

Then click “Credentials” under “APIs & auth” to generate an API key.
Now to create an Google API key, click Create new Key under Public API access.

Generate New Key - GCM

Generate New Key - GCM

When you should see the following popup, click the Server Key button.

Server Key - GCM

Server Key - GCM

The next page will contain a field for IP address whitelisting - it is very important to leave this blank, else AppVirality will be unable to use the key

Keep IP whitelist clean

Keep IP whitelist clean

Click the Create button and your key shall appear

API Key generated - GCM

API Key generated - GCM

STEP 2 Register device and receive Push Notifications


If you are already using push notifications, you must have already obtained user push registration ID which is required to send the push notification.
Send the user push registration ID to AppVirality using the following code

UserDetails userDetails = new UserDetails();
userDetails.setPushToken(token);
appVirality.updateAppUserInfo(userDetails, null);

If you are not using the Android push notifications already, or do not have Android Push Registration ID, please follow these steps to set up your App - so as to receive the push notifications

Get GCM/FCM Push Token

Create a new class GCMRegistrationIntentService.java extending IntentService class to register the device with GCM/FCM and hence acquire the Push Token . Override the onHandleIntent(Intent intent) method, fetch the Push Token inside it and store it on local storage.

We already have given this file in the AppViralityTestApp module, so, please find this file and change the following line by replacing the text YOUR-SENDER-ID with your Google Project ID i.e Sender ID - the same whose creation we displayed in Step 1.

static String SENDER_ID = "YOUR-SENDER-ID";

After setting the sender ID, start the GCMRegistrationIntentService.java Service to register the device with GCM and get the user push registration ID

Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent);

This service will fetch the user push registration ID from GCM and update the same with AppVirality automatically.

Receiving & Building Push Notifications

In order to receive and build the push notification, we have provided a Service Class with the name GcmCustomListenerService.java which is available in AppviralityTest module, it extends GcmListenerService class whose onMessageReceived() method we need to override to receive the notification data and further creating the notification using it. Implement the same & get going!

import com.google.android.gms.gcm.GcmListenerService;
...
public class GcmCustomListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
handleNotificationIntent(data);
}
...
}

Refreshing GCM/FCM Token

Create GcmInstanceIDListenerService class extending InstanceIDListenerService and override onTokenRefresh() method to get notified when the GCM/FCM token needs to be refreshed, and thereafter refresh the token.

import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService;
public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and update the same on over server.
Intent intent = new Intent(this, GcmRegistrationIntentService.class);
intent.putExtra("should_refresh_token", true);
startService(intent);
}
}

Setting up your AndroidManifest.xml

Add the following permissions required to receive the push notifications.

1. To set up your permissions, replace YOUR_PACKAGE_NAME with your own app’s package name in the the following snippet, and add them to your AndroidManifest.xml inside the <manifest/> tag:

<permission
android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

2. Now you’ll need to declare a <receiver/> element which will receive inbound notifications. Replace YOUR_PACKAGE_NAME with your own app’s package name in the the following snippet, and add it to your AndroidManifest.xml inside the <application/> tag:

<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
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="YOUR_PACKAGE_NAME" />
</intent-filter>
</receiver>

3. Declare GcmCustomListenerService, subclass of GcmListenerService, to be used for receiving the notification data and creating the notification.

<service
android:name="GcmCustomListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>

4. Declare GcmInstanceIDListenerService, subclass of InstanceIDListenerService, to be used for handling Instance ID service notifications on token refresh.

<service
android:name="GcmInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>

5. Declare Service Class GcmRegistrationIntentService, to be used for registering a user on GCM/FCM so push notifications can be sent to them.

<service
android:name=".GcmRegistrationIntentService"
android:exported="false" />

STEP 3 Configure Push Notifications


All the above steps were simply preparing your app for the Push Notifications. Now you shall configure the Push Notification settings on the AppVirality Dashboard.

Set your Google API Key

For AppVirality to send Google Cloud Messaging/Firebase Cloud Messaging notifications on your behalf, you will need to submit the Google API key(for GCM) or Legacy Server key(for FCM) generated by following step 1 above.
This implies the New API Key you generated for the Public API Access section in your Google Cloud Platform.
In case of FCM, you can find the Legacy Server Key from under Project Settings -> Cloud Messaging for your app on the firebase console.

The way to do that is as simple.

  • App Details on your AppVirality Dashboard by clicking on left menu,
  • Click on App Settings
  • Switch to PUSH tab

Now, configure the push credentials for your app by adding the GCM API Key/Legacy Server Key for Android in the respective field.

Add Push Key

Add Push Key

Push Message Template

Push Messages are pretty slick way to engage your users.

While there could be a varied set of use cases for notifying users (differs app to app), we have covered a few basic ones & have put in place a set of few templates.

You can select any of the 4 predefined templates, or simply move ahead with defining your own custom push template. Do remember to use the Replacement Tokens and add that personal touch.

Configuring Push Notifications

Configuring Push Notifications

Having stated the above, you must know that AppVirality allows you to use Third Party Push Messaging services, and hence enables you with the respective customization hooks.
The default Notification Service is set to GCM or APN, as may be appropriate for Android or iOS

  • If you employ any such third party push notification service, simply toggle the Notification Service to OTHER.
  • Next, add the Push Notification URL for your 3rd party push messaging service.
  • Hereafter, you may need to add Headers for additional validations - which is also available as a multiple key-in field set. Add as many as you may like.
  • Save your changes & get rolling!

Here is how all this might look/appear like:

3rd Party Push services

3rd Party Push services

In case of Third party notification service, set the post data within the message text-box

Custom Message Template for 3rd Party Push Messaging services

Custom Message Template for 3rd Party Push Messaging services

In case of GCM notification service, compose your desired message in the text-box.
The push notification receiver will receive the message as part of data object as below.

{
"message":"YOUR DESIRED MESSAGE"
}

If complex JSON data need to be sent, then compose the message according to the your custom JSON format.

{
"message":"Custom Message to user",
"sound":"default",
"image":"refer1.jpg"
}

That completes your Push Notification Configuration


Wanna Test your Configuration…??? Head down to the Testing Guide