Ir para o conteúdo

Android Client Share Firebase Cloud Messaging Integration

In Google's infrastructure, it is possible to use only one channel for push messages per application. Because of this, DialMyApp android library needs to have a shared push mechanism together with a hosting application. To make this possible, DialMyApp implemented hooks in DMA library, that allows DialMyApp also receive push messages. DialMyApp parses only messages related to itself (the one that contains "dma_library_data" inside it).

{  "data":{    "dma_library_data":{
       ...

DialMyApp needs the business' android application Firebase ServerKey to be able to send Push Notifications.

DMA library hook to subscribe device on Push notifications

DMA Android Library provides a callback to utilize a shared push mechanism.

On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

You can access this token by extending FirebaseMessagingService and overriding onNewToken(String token). FCM triggers the method onNewToken(String token) each time when a new token is generated.

In order to query the current token manually please call FirebaseMessaging.getInstance().getToken().

It is necessary to subscribe a device by sending current FCM token to DMA server by calling FcmHandler.onTokenReceived() method.

Recommended places to share the token with DMA SDK are "onTokenReceived" and manually on the app launch. DMA SDK handles cases when the token has been shared successfully but the app tries to do that again with the same token.

API signature for onTokenReceived:

/**
* @param token    - FCM current token
* @param bundle   - Put your additional arguments(user's phone number, etc)
* @param context  - Is required for further processing of payload data
* @param callback - Override methods of this interface to receive the result of "subscribe" action
*/
public static void onTokenReceived(Context context,
                                  String token,
                                  @Nullable Bundle bundle,
                                  @Nullable final OnSubscribePushesListener callback) {
   ...
/**
* @param token    - FCM current token
* @param bundle   - Put your additional arguments(user's phone number, etc)
* @param context  - Is required for further processing of payload data
* @param callback - Override methods of this interface to receive the result of "subscribe" action
*/
FcmHandler.onTokenReceived(
    context: Context,
    token: String,
    bundle: Bundle?,
    callback: OnSubscribePushesListener?
)

Because the token could be rotated after initial startup, you are strongly recommended to monitor changes to the token and to retrieve the latest updated registration token to refresh it on DMA Server every time after token change.

Token registration example

import org.mbte.dialmyapp.messages.fcm.FcmHandler;

class FcmService extends FirebaseMessagingService {
   @Override
   public void onNewToken(@NonNull String token) {
       super.onNewToken(token);
       FcmHandler.onTokenReceived(this, token, null, null);
   }
...
public void queryToken(Context context) {
   FirebaseMessaging.getInstance().getToken().addOnSuccessListener(new OnSuccessListener<String>() {
       @Override
       public void onSuccess(String token) {
           FcmHandler.onTokenReceived(context, token, null, null);
       }
   });
}
import org.mbte.dialmyapp.messages.fcm.FcmHandler

internal class FcmService : FirebaseMessagingService() {
   override fun onNewToken(token: String) {
       super.onNewToken(token)
       FcmHandler.onTokenReceived(this, token, null, null)
   }
...
fun queryToken(context: Context?) {
   FirebaseMessaging.getInstance().token.addOnSuccessListener { token ->
       FcmHandler.onTokenReceived(
           context,
           token,
           null,
           null
       )
   }
}

DMA library hook for Processing of payload data

After successfully sending a token to the DMA server it is necessary to provide access to incoming push messages.

FCM triggers method onMessageReceived(RemoteMessage message) each time when push message is received. Please connect FCM and DMA library by calling method FcmHandler.onNewMessageReceived(payload) and passing a payload as a parameter from the message argument. Perform this invocation inside overridden method onMessageReceived in extended from FirebaseMessagingService class:

class FcmService extends FirebaseMessagingService {
   @Override
   public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
       super.onMessageReceived(remoteMessage);
       FcmHandler.onNewMessageReceived(this, remoteMessage.getData().toString());
   }
...
internal class FcmService : FirebaseMessagingService() {
   override fun onMessageReceived(remoteMessage: RemoteMessage) {
       super.onMessageReceived(remoteMessage)
       FcmHandler.onNewMessageReceived(this, remoteMessage.data.toString())
   }
...

FcmHandler.onNewMessageReceived returns true in case payload is from DialMyApp and targeted for processing for FcmHandler.

When there is no access to native FirebaseMessagingService

  1. Request the firebase token programmatically using public API of your plugin/library, etc and share it with DMA SDK using function FcmHandler.onTokenReceived() or appropriate wrapper function (for ReactNative, Cordova, Flutter, etc).

  2. DMA SDK might be configured by demand with the special DmaFcmReceiver class which handles receiving DMA's push messages in the background. Hence, no need to implement FcmHandler.onNewMessageReceived() handling on the app side. Please inform us about the possibility to make changes in the native push service class of your application - we'll provide appropriate SDK configuration.