HomeAppVirality DocsAdvanced ConfigurationsAndroid Push Notification Configuration

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.

 


Enabling Google Cloud Messaging in your Google API Console

 


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

 

Create a new class GCMRegistrationIntentService.java extending IntentService class to register the device with GCM 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.

NOTE: If you are using the GCMRegistrationIntentService.java to register the device as shown above, you don’t have to call the updateAppUserInfo() SDK method to update the Push Token with AppVirality.

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 Token

Create GcmInstanceIDListenerService class extending InstanceIDListenerService and override onTokenRefresh() method to get notified when the GCM 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);
    }
}

 

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 so push notifications can be sent to them.

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


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.

 

For AppVirality to send Google Cloud Messaging notifications on your behalf, you will need to submit the Google API key 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.

The way to do that is as simple.

Navigate to:

  • 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 for Android in the respective field.

Add Push Key
Add Push Key

 

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.

Campaign Ready?

You can only configure Push Messages & Templates for the same, when you have an active campaign in your AppVirality dashboard

 

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

 

Push Template Status ON?

One must remember to toggle or switch ON the respective push template, to ensure that particular template gets fired.
Also, remember to keep an eye on the platform: iOS or Android, for which you switched on the template.

 

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

 

Was this article helpful to you? Yes No