PushAmplify with FCM

Use PushAmplify along with FCM

1. Introduction

There are multiple strategies to use PushAmplify along with FCM:

  • Send push to both PushAmplify and FCM using respective Push APIs. Now, app can receive push from either of the service. Show the push from which ever service delivers it first and suppress the push from the other service.

2. Sending push to both the services

2.1 Send push to PushAmplify

First send push to PushAmplify using the Push API. On success, the API responds with a uniqid for each of the push. Example:

Request to http://api.pushamplify.com/send with the following post body:

{
    "apikey": "a3af1b4b-8ad7-4fe8-995b-14abd68f01d3",
    "deviceids": ["44267df9-d395-49b8-a46b-8233a5aa9b98"],
    "payload": {
        "title": "My push title",
        "body": "My push body"
    }
}

Response:

{
    "status": "success",
    "uniqids": ["195f1f93-1a71-4118-b0a0-e66e79cc7375"]
}

You can see the uniqids key containing the uniqids.

2.2 Send push to FCM

Now add the above uniqids to the push payload of FCM and send the push using FCM too. Example:

Request to http://fcm.googleapis.com/fcm/send with the following post body:

{
    to: "<fcm_token>"
    "data": {
        title: "My push title",
        body: "My push body",
        __uniqid: "195f1f93-1a71-4118-b0a0-e66e79cc7375"
    }
}

You can see the __uniqid key being added to the body.

3. Push received by PushAmplify

Once push is received by PushAmplify SDK and acknowledged by your message handler by way of returning true (please refer Implement your PushHandler), PushAmplify automatically marks it as received at its end to not deliver it again.

4. Push received by FCM

Once push is received by FCM message handler, you can check if the push is already received by your app through PushAmplify using the following code:

PushAmplifyClient.isReceived(Context ctx, String uniqId)

The ctx is an application context, and uniqId is the uniqid of the push that has been added to the FCM push payload above.

If push is not received, you can handle the push and finally mark it as received at PushAmplify so it does not deliver it.

PushAmplifyClient.markReceivedByFCM(Context ctx, String uniqId);

With both the above methods, example implementation of your FCM message handler can be like below:

// This is your FCM message handler
public class MyAppFCMListener extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage message){
        String uniqId = message.getData().get("__uniqid")
        if (!PushAmplifyClient.isReceived(ctx, uniqId) {
            // Handle your FCM push here
            // Finally mark the push received in PushAmplify so it doesnot deliver it again
            PushAmplifyClient.markReceivedByFCM(getBaseContext(), uniqId);
        }
    }
}