Documentation > Android

Custom Android Push Notifications handling

Using your custom implementation

If you have your custom implementation for managing Android push notifications, you can inform CleverTap about the user’s GCM registration ID:

String gcmRegId = gcm.register(projectNumber);, true);

copy Copied

Add the following code to your GCM broadcast receiver:

Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging. MESSAGE_TYPE_MESSAGE.equals(messageType)) {
  CleverTapAPI.createNotification(context, extras);

copy Copied

Using Multiple Android Push Notification Providers

CleverTap plays well with other Android push notification providers. You can configure your app to work with multiple push notification providers.

In the below example, we use Parse as the other provider. The idea is to create your own GCM broadcast receiver, and then notify both CleverTap and Parse upon receiving a push message.

Subclass WakefulBroadcastReceiver as follows:

public class MyCustomGcmBroadcastReceiver extends WakefulBroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        CleverTapAPI.createNotification(context, intent.getExtras());
        new com.parse.GcmBroadcastReceiver().onReceive(context, intent);
copy Copied

Remove the CleverTap and Parse GCM broadcast receivers from your AndroidManifest.xml

Now, add your new broadcast receiver:

        <action android:name=""/>
        <action android:name=""/>

        <category android:name="com.mypackage"/>
copy Copied

Replace com.mypackage with your app package name. Replace with the correct package name which contains the newly created class, MyCustomGcmBroadcastReceiver

You should now be able to confirm that push notifications from CleverTap and Parse are now working.

For FCM:

Subclass FirebaseMessagingService:

public class MyFcmMessageListenerService extends FirebaseMessagingService {

    public void onMessageReceived(RemoteMessage message){
        try {
            if (message.getData().size() > 0) {
                Bundle extras = new Bundle();
                for (Map.Entry<String, String> entry : message.getData().entrySet()) {
                    extras.putString(entry.getKey(), entry.getValue());

                NotificationInfo info = CleverTapAPI.getNotificationInfo(extras);

                if (info.fromCleverTap) {
                    CleverTapAPI.createNotification(getApplicationContext(), extras);
                } else {
                    // not from CleverTap handle yourself or pass to another provider
        } catch (Throwable t) {
           Log.d("MYFCMLIST", "Error parsing FCM message", t);
copy Copied

Add your service to the AndroidManifest.xml in place of the CleverTap FcmMessageListenerService:

        <action android:name=""/>
copy Copied

Replace com.your.package with the correct package name which contains the newly created class, MyFcmMessageListenerService

Structure of CleverTap GCM Payload

Key Type Description
wzrk_pn If present, this notification is sent from CleverTap
wzrk_id String Open rate tracking ID (can be empty/not present)
wzrk_bp String If present, the value will be a URL to an image that needs to be shown in the notification
wzrk_sound If present, it signifies that the default Android notification sound must be played
nt String Notification title. If absent or empty, fallback to the app name
nm String Notification body. If absent or empty, ignore this notification
wzrk_dl String If present, this is a deep link that must be followed at the time of notification open
wzrk_d If present, ignore this notification
ico String If present and non empty, it contains the URL to an image that must be used as the notification small icon

If the field nm is empty, ignore the notification. CleverTap sends out a dummy notification with the nm field empty to test for app uninstalls. In addition to the above, attach all keys starting with wzrk_ to your notification extras