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/FCM registration ID

a. For GCM:

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
b. For FCM:


String fcmRegId = FirebaseInstanceId.getInstance().getToken();
cleverTapAPI.data.pushFcmRegistrationId(fcmRegId,true);
copy Copied

Tracking android push notifications:
Please ensure that the following method is called in every calling activity to enable tracking of android push notifications:

@Override
protected void onNewIntent(Intent intent) {
     try {
         CleverTapAPI.getInstance(this).event.pushNotificationEvent(intent.getExtras());
     } catch (Throwable t) {
         t.printStackTrace();
     }
     super.onNewIntent(intent);
}
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 {

    @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

To track the push notification events and deeplinks add the following receiver in your AndroidManifest.xml

<receiver
    android:name="com.clevertap.android.sdk.CTPushNotificationReceiver"
    android:exported="false"
    android:enabled="true">
</receiver>
copy Copied

Structure of CleverTap GCM Payload

Key Type Description
wzrk_pn N/A If present, this notification is sent from CleverTap
wzrk_id String Open rate tracking ID (can be empty or it might not be present)
wzrk_bp String If present, the value will be a URL of an image, that needs to be displayed in the notification
wzrk_sound Boolean or String If present, it signifies that the default or custom 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 N/A If present, ignore this notification
ico String If present and not empty, it contains the URL of an image, that must be used as the small notification icon
wzrk_pivot String If present and not empty, it contains the URL of an image, it signifies the type of variant in A/B campaigns

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 (For example, wzrk_id is used to track notification opens).