Configuring GCM
Before configuring GCM please make sure General Push Configuration is completed.
Please make sure you have a completed GCM Setup before proceeding further time.
Adding GCM Dependency
By default the SDK ships with MoEngage's Firebase SDK. To use GCM add the below dependency in the app level build.gradle
file.
implementation ('com.moengage:moe-android-sdk:+'){
exclude group: 'com.moengage', module: 'moe-push-firebase'
}
implementation 'com.moengage:moe-push-gcm:3.2.01'
ADDING PERMISSIONS
For GCM to deliver push notifications there are certain permissions mandatory in the manifest file, add them if not added already
<!-- If you have specified applicationId in build.gradle file then replace [YOUR_PACKAGE_NAME] with applicationId-->
<permission android:name="[YOUR_PACKAGE_NAME/applicationId].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[YOUR_PACKAGE_NAME/applicationId].permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Adding Listener Service
Using MoEngage SDK's Listener Service
<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" />
<category android:name="[YOUR_PACKAGE_NAME/applicationId]" />
</intent-filter>
</receiver>
<!-- Listener for Receiving push payload from GCM -->
<service
android:name="com.moengage.worker.MoEGCMListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</service>
<!-- Listener for refresh token callback -->
<service
android:name="com.moengage.receiver.MoEInstanceIDListener"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
In case you want to register for Push via MoEngage and have your own listener for receiving push payload then add only the refresh token callback listener and pass the push payload to MoEngage SDK as shown below.
Using your own listener service
In case you have multiple push providers you should have only one service in your manifest file and pass on the payload to all providers. To pass payload to MoEngage SDK use the following snippet
public class MyGCMListenerService extends GcmListenerService {
@Override public void onMessageReceived(String from, Bundle data) {
if( null == data)return;
if( MoEngageNotificationUtils.isFromMoEngagePlatform(data)){
//If the message is not sent from MoEngage it will be rejected
PushManager.getInstance().getPushHandler().handlePushPayload(getApplicationContext(), data);
}else{
//your logic
}
}
}
Note
When using play services version greater than 7.5, at any point your application's manifest should have only one service with intent filter com.google.android.c2dm.intent.RECEIVE and one service with intent filter com.google.android.gms.iid.InstanceID. If there are more than one service with either of the intent filters it could lead to poor notification delivery.
Testing Push Notification
Refer to this link to read more about how to create and test push notifications.
Updated over 6 years ago