Documentation > Android

Custom Push Notifications handling

Using your custom implementation

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

String gcmRegId = gcm.register(projectNumber);
cleverTap.data.pushGcmRegistrationId(gcmRegId, 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 Push Notification Providers

CleverTap plays well with other 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 {

    @Override
    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:

<receiver
        android:name=“com.mypackage.path.to.MyCustomGcmBroadcastReceiver"
        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="com.mypackage"/>
    </intent-filter>
</receiver>
copy Copied

Replace com.mypackage with your app package name. Replace com.mypackage.path.to 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 {

    @Override
    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:

<service
    android:name="com.your.package.MyFcmMessageListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
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