You can configure your Android project to use Leanplum push services with other custom or third-party push notification services, whether you use Firebase Cloud Messaging or Google Cloud Messaging. Generally, it can be done by creating custom classes that extend the correct Leanplum push messaging classes.
Create a custom class to receive push messages
Regardless of whether you are using GCM or Firebase, you will need to create a custom class (if you haven't already) to define the behavior for your project after receiving a Push Message. The only difference (between GCM and Firebase) is which class you should extend.
For GCM, the new class .Custom_PushListener
must extend the LeanplumPushListenerService
class (which is extending GcmListenerService
).
public class Custom_PushListener extends LeanplumPushListenerService {
// ...
@Override
public void onMessageReceived(String var, Bundle notificationPayload) {
// This ensures the Leanplum code is executed once a Push Notification message is received
super.onMessageReceived(var, notificationPayload);
// The remaining code will be executed in addition to the Leanplum default behavior
Log.i("### ", "Push received");
}
}
For Firebase, the new class .Custom_FirebaseMessagingService
must extend the LeanplumPushFirebaseMessagingService
class (which is extending FirebaseMessagingService
).
public class Custom_FirebaseMessagingService extends LeanplumPushFirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.i("### ", "Firebase message received");
}
}
Note: For both GCM and Firebase, your custom class must include the onMessageReceived
method, which should override its parent method but still call super.onMessageReceived()
. This will make sure the Leanplum code in the extended class is executed when the push notification is received. You can place any custom code within this method after this call.
We will invoke this custom class later with a service in the Android Manifest.
Create a custom class to handle push token refreshes (Firebase only)
Because Leanplum needs to monitor any changes to the push token with Firebase, you must register a service around the com.google.firebase.INSTANCE_ID_EVENT
intent. To do that, you first must create a .Custom_PushFcmListenerService
class that extends the LeanplumPushFcmListenerService
class (which is extending FirebaseInstanceIdService
).
Note: This class must have an onTokenRefresh
method that overrides its parent and also calls super.onTokenRefresh()
to fetch the updated Instance ID token and notify Leanplum app's server of any changes.
public class Custom_PushFcmListenerService extends LeanplumPushFcmListenerService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
}
}
Invoke the custom class(es) in your Manifest
Assuming you have already implemented our SDK and modified your Application class and AndroidManifest.xml
file as documented in the Android setup, you will just need to make a few modifications to your Manifest in order to utilize the push services.
For GCM, you'll only need to modify a single service for the com.google.android.c2dm.intent.RECEIVE
intent so that it will invoke the .Custom_PushListener
class (created above).
Change the android:name
attribute to point to your custom class.
<service
<!-- android:name="com.leanplum.LeanplumPushListenerService" -->
android:name=".Custom_PushListener"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
For Firebase, you will need to edit two services for the com.google.firebase.MESSAGING_EVENT
and com.google.firebase.INSTANCE_ID_EVENT
intents so that they invoke the .Custom_FirebaseMessagingService
and .Custom_PushFcmListenerService
classes respectively.
Change the android:name
attributes to point to your custom classes.
<service
<!-- android:name="com.leanplum.LeanplumPushFirebaseMessagingService" -->
android:name=".Custom_FirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
<!-- android:name="com.leanplum.LeanplumPushFcmListenerService" -->
android:name=".Custom_PushFcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Sample Projects on Github
A sample GCM project demonstrating multiple push services is available here, and you can see its manifest file here.
A sample Firebase project is available here, and you can see its manifest file here.