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.
If you are already using push notifications, you must have already obtained user push registration ID which is required to send the push notification. 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/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. After setting the sender ID, start the GCMRegistrationIntentService.java Service to register the device with GCM and get the user push registration ID This service will fetch the user push registration ID from GCM and update the same with AppVirality automatically. 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! 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. 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: 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: 3. Declare GcmCustomListenerService, subclass of GcmListenerService, to be used for receiving the notification data and creating the notification. 4. Declare GcmInstanceIDListenerService, subclass of InstanceIDListenerService, to be used for handling Instance ID service notifications on token refresh. 5. Declare Service Class GcmRegistrationIntentService, to be used for registering a user on GCM/FCM so push notifications can be sent to them. 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/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. The way to do that is as simple. Now, configure the push credentials for your app by adding the 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 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. One must remember to toggle or switch ON the respective push template, to ensure that particular template gets fired. 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. Here is how all this might look/appear like: In case of Third party notification service, set the post data within the message text-box In case of GCM notification service, compose your desired message in the text-box. If complex JSON data need to be sent, then compose the message according to the your custom JSON format. Wanna Test your Configuration…??? Head down to the Testing Guide
Send the user push registration ID to AppVirality using the following codeUserDetails userDetails = new UserDetails();
userDetails.setPushToken(token);
appVirality.updateAppUserInfo(userDetails, null);
static String SENDER_ID = "YOUR-SENDER-ID";
Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent);
Receiving & Building Push Notifications
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
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);
}
}
<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" />
<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>
<service
android:name="GcmCustomListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="GcmInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".GcmRegistrationIntentService"
android:exported="false" />
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.Navigate to:
Campaign Ready?
Push Template Status ON?
Also, remember to keep an eye on the platform: iOS or Android, for which you switched on the template.
The default Notification Service is set to GCM or APN, as may be appropriate for Android or iOS
The push notification receiver will receive the message as part of data object as below.
{
"message":"YOUR DESIRED MESSAGE"
}
{
"message":"Custom Message to user",
"sound":"default",
"image":"refer1.jpg"
}
That completes your Push Notification Configuration