Push notifications
This documentation only describes how to configure the SDK for our push notifications feature. If you want to learn about the feature and the full set of implementation requirements, see the main feature documentation:
How do push notifications work?
For Android, we support the use of Firebase Cloud Messaging (FCM) push notifications to notify the end-user when an operation has started.
To learn more about how FCM works, see the Firebase Cloud Messaging page in the Android developer documentation.
Configure the SDK
Prerequisites
Before you start, you need make sure that you have set up support for FCM push notifications. This documentation assumes that the following steps have been completed:
- Add Firebase to your Android project.
- Set up a Firebase Cloud Messaging client on Android
- Ensure that:
FcmMessagingServiceextendsFirebaseMessagingService.- You have added the required entries for FCM to
AndroidManifest, as described in the FCM documentation.
1. Add new service entry to AndroidManifest.xml
Verify that you have added a new service entry on your AndroidManifest.xml file.
<service
android:name="com.encapsecurity.android.standardapp.fcm.FcmMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
2. FcmMessagingService
Set push token on the Encap controller
- When FCM invokes the
onNewToken(newToken: String)method ofFirebaseMessagingService, the service should set the new push token on theEncapController.- Kotlin
- Java
Example: Set push token on Encap controlleroverride fun onNewToken(newToken: String) {
super.onNewToken(newToken)
controller.config = EncapConfig.Builder(controller.config)
.setPushToken(newToken)
.build()
}Example: Set push token on Encap controller@Override public void onNewToken(String newToken) {
super.onNewToken(newToken);
controller.setConfig(new EncapConfig.Builder(controller.getConfig())
.setPushToken(newToken)
.build());
} - The
onNewToken()method is called when a token is first generated and again if the token changes. This happens when:- The app is restored on a new device.
- The end-user uninstalls/reinstalls the app.
- The end-user clears app data.
Receiving messages
When FCM invokes the onMessageReceived(remoteMessage: RemoteMessage) of the FcmMessagingService:
- Create an intent with the action
com.google.firebase.MESSAGING_EVENT. - Set the intent's extra data with
remoteMessage.toIntent(). - Send the intent to the
Activity, either directly or via a notification with aPendingIntent()if the app is in the background.
- Kotlin
- Java
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val intent = Intent(this, MainActivity::class.java).apply {
action = "com.google.firebase.MESSAGING_EVENT"
putExtras(remoteMessage.toIntent())
}
// If application is in foreground, immediately send intent to activity
// Otherwise, display a notification instead
if(appInForeground){
startActivity(intent)
} else {
displayNotification(intent)
}
}
@Override public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.setAction("com.google.firebase.MESSAGING_EVENT");
intent.putExtras(remoteMessage.toIntent());
// If application is in foreground, immediately send intent to activity
// Otherwise, display a notification instead
if(appInForeground){
startActivity(intent);
} else {
displayNotification(intent);
}
}
3. Activity
- When the
Activityreceives an intent inonNewIntent(Intent)oronCreate(), 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). - If the push message is a start message, then examine whether it starts an activation or authentication, and call the Encap API's corresponding start method.
- Kotlin
- Java
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
processIntent(intent)
}
private fun processIntent(intent: Intent) {
try {
if ("com.google.firebase.MESSAGING_EVENT" == intent.action) {
// Launched from FCM.
processPushMessage(intent)
} else {
// Your other notifications handling
}
} catch (ex: Exception) {
Log.d(TAG, "processIntent: caught exception: ", ex)
}
}
private fun processPushMessage(intent: Intent) {
val pushMessage = PushMessageFactory.createFromIntent(intent)
if (pushMessage.isStartMessage) {
if (pushMessage.isActivationMessage) {
// Handle activation
} else if (pushMessage.isAuthenticationMessage) {
// Handle authentication
} else if(pushMessage.isRecoveryPerformedNotification) {
// Handle recovery performed
}
else {
Log.d(TAG, "unexpected message type")
}
}
}@Override public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
processIntent(intent);
}
private void processIntent(Intent intent) {
try {
if ("com.google.firebase.MESSAGING_EVENT".equals(intent.getAction())) {
// Launched from FCM.
processPushMessage(intent);
} else {
// Your other notifications handling
}
} catch (Exception ex) {
Log.d(TAG, "processIntent: caught exception: ", ex);
}
}
private void processPushMessage(Intent intent) throws java.net.URISyntaxException {
PushMessage pushMessage = PushMessageFactory.createFromIntent(intent);
if (pushMessage.isStartMessage()) {
if (pushMessage.isActivationMessage()) {
// Handle activation
} else if (pushMessage.isAuthenticationMessage()) {
// Handle authentication
} else if(pushMessage.isRecoveryPerformedNotification()) {
// Handle recovery performed
}
else {
Log.d(TAG, "unexpected message type");
}
}
} - Create a push message of it using
For more details on the implementation, see the com.encapsecurity.encap.example.android.testapp.util.FcmMessagingService class.
Custom push payload
You can send a custom payload in the push to the device.
If the custom payload is sent, it can be read by creating a PushMessage with PushMessageFactory.createFromIntent() either:
- In the
FcmMessagingServiceclass withinonMessageReceived(). - In in the Activity within
onNewIntent().
- Kotlin
- Java
val pushMessage = PushMessageFactory.createFromIntent(intent)
val payload: String? = pushMessage.customPayload
PushMessage pushMessage = PushMessageFactory.createFromIntent(intent);
String payload = pushMessage.getCustomPayload();