# Push notification

# Overview

With Firebase Cloud Messaging (FCM) push notifications, the app can be started automatically from the Encap server.

# Set up FCM push notifications

You can add support for FCM by following the steps outlined in Firebase Cloud Messaging - to set up FCM for Android (opens new window), in the Android developer documentation.

This documentation assumes that:

  • FcmInstanceIDService extends FirebaseInstanceIdService.
  • FcmMessagingService extends FirebaseMessagingService.
  • AndroidManifest has added the proper entries for FCM, as described in the FCM documentation.

# FcmInstanceIDService

When FCM invokes the onTokenRefresh() method of FcmInstanceIDService, the service should set the new push token on the EncapController. This is done by calling setPushToken(String) of EncapConfig.Builder, and setConfig(EncapConfig) on the EncapController with the EncapConfig from the Builder's build() method.

The onTokenRefresh() method is called if InstanceID token is updated.

This happens in the following cases:

  • App deletes InstanceID.
  • App is restored on a new device.
  • User uninstalls/reinstall the app.
  • User clears app data.

# FcmMessagingService

When FCM invokes the onMessage(RemoteMessage) of the FcmMessagingService:

  • Create an intent with action com.google.firebase.MESSAGING_EVENT.

  • Set the intent's extra data with Encap key/value data for the key PushMessage.URL_KEY and data extracted from the getData().get(PushMessage.URL_KEY) of the RemoteMessage.

intent.putExtra(PushMessage.URL_KEY, remoteMessage.getData().get(PushMessage.URL_KEY));
  • Send the intent to the Activity, either directly or via a notification.

# Activity

When the Activity receives an intent in the getIntent() and onNewIntent(Intent) of onCreate(), you should examine the intent.

If the intent is an FCM intent with action com.google.firebase.MESSAGING_EVENT, then:

  • Create a push message of it using PushMessageFactory.createFromIntent(Intent).

  • Transfer the pushSessionId from the push message to the EncapAPI using the Controller's setPushSessionId.

  • If the push message is a start message, then examine whether it starts an activation or authentication, and call the EncapAPI's start method.

See the com.encapsecurity.encap.example.android.testapp.fcm classes and the TestPushActivity for more details.

# Custom push payload

You can send a custom payload in the push to the phone.

If the string is sent, it can be read by creating a PushMessage in onMessageReceived().

Last updated: 04/03/2024 15:17 UTC