This article will help you to set up push notification in Android applications.
VoxImplant push notifications for Android are based on FirebaseCloud Messaging (FCM) platform and implemented as "data messages". With the help of notifications, a device can be awakened to show a new message in user’s conversation(s) and an app can be started before a call arrives.
Push notifications for calls are sent with high priority and delivered without delay.
The following tutorial explains the code needed to handle push notification and describes additional steps that should be performed to have a working Push Notifications solution for your mobile applications.
Before you start, please check that your project meets FCM requirements.
Add Firebase to the Android project
To add push notifications to the Android project, it is required to add Firebase to the project.
Follow the FCM instructions and recommendations to:
The Voximplant Control Panel allows you to add only one certificate of one type. If you are developing an application and need to test push notifications in both Debug and Release modes, add the same GOOGLE certificate with your application bundle + specific suffixes, for example, “.dev”, “.prod”.
The Voximplant Cloud does not validate the bundle id, we use this parameter here only to identify the certificate needed to send a push notification. So, you don’t need to change the real bundle id, but only “mark” it for the Voximplant Cloud.
Do not forget to initialize the SDK correctly for Debug and Release configuration and set the suffix of the bundle id depending on the configuration.
Modify your VoxEngine scenario (for calls)
Add certificates to the Voximplant cloud
- Log in to the Firebase console with your Google account you have connected earlier in this tutorial.
- Go to Project Settings.
- Navigate to the Service Accounts tab.
- Navigate to Google Cloud console by clicking “Manage service accounts permissions”.
- Generate a key for the firebase-adminsdk account.
- Log in to the Voximplant control panel.
- Go to your application, then switch to the Push Certificates tab.
- Click on the Add Certificate in the upper right corner.
- Select GOOGLE from the Certificate for list.
- Upload Service account JSON file.
- Click on the Add button.
The Voximplant Platform supports sending push notifications to several Android applications with different package names within the same Voximplant application. If it is required to add several FCM certificates to the same Voximplant application, you have to set the application package name while adding the certificate.
If a package name is specified with a certificate, you must set the same package name when initializing the SDK.
Provide a token to the Voximplant cloud
The Voximplant Cloud requires a device token to send a push notification. The token should be obtained from Firebase API on the application side and registered via IClient.registerForPushNotifications API:
Register the token in these cases:
Token has been updated by
Some other Voximplant user has logged in on the device
Monitor token refresh
The application should also provide a token if Firebase refreshes it. Override the onNewToken method of previously extended FirebaseMessagingService and pass a new token to the Android SDK.
Handle push notifications (calls)
Push notifications as “data messages” are received by means of the service that overrides FirebaseMessagingService. You need to override the onMessageReceived method to get the push notification data. The onMessageReceived method is invoked in any application state (foreground or background).
We recommend the following process for handling call push notifications:
IClient.handlePushNotification API may be called in any ClientState, but to receive an ICall instance of an incoming call, it is required to connect to the Voximplant Cloud and log in. After you do that, the IClientIncomingCallListener.onIncomingCall event is invoked to provide access to the ICall object.
Call push notifications are sent automatically. Call the callUser method in the VoxEngine scenario and push notification is automatically sent to the app. Thus, the app can wake up, connect to Voximplant, log in, and accept the call.
Use an analytics label to monitor push delivery status in real time inside Firebase console via FCM reporting dashboard.
Subscribe for IM push notifications for instant messaging
The Voximplant Cloud can send push notifications for IP messaging when one of the following events occurs:
Handle push notifications (IP messaging)
Push notifications as “data messages” are received by means of the service that overrides FirebaseMessagingService. You need to override the onMessageReceived method to get the push notification data.
The onMessageReceived method is invoked in any application state (foreground or background).
To ensure that this is a Voximplant IM push notification, check the “voximplant-im” signature in the push data.
A push notification payload contains the following information:
New or edited message UUID
Conversation UUID the message belongs to
Initiator’s user id
Sequential number of the event in the conversation
Please note that the IMessengerPushNotificationProcessing interface only converts data message to an IMessageEvent object with no modifications of it or updates of other Messenger objects instances.
Use tokens or keys for better security
Voximplant supports 3 ways of the authorization from SDK:
- With a username and a password
- With a username and a token
- With a one-time login key
Saving the password is not secure, so we would recommend that you use the "token" or "key" approach.
The token is received from an event after a successful login and can be saved and used later instead of the password.
The "login key" approach allows you to completely avoid the use of password on the client-side by offloading it to your backend.