Voximplant. Blog

Push Notifications for Android

VoxImplant push notifications for Android are based on FirebaseCloud Messaging (FCM) platform and implemented as the “data messages”. Using notifications, a device can be awakened and an app started before a call arrives. Our SDK requires Android API 16 (4.1), while push notifications support also requires Play Services 10.0.1 or higher to be available on the device. Our Android SDK contains a sample application with all the code needed to handle push notification. Following tutorial explains that code and describes additional steps that should be performed to have a working Push Notifications solution for your mobile applications.

Push notifications are available since Android SDK version 2.1.0

Add Firebase to the Android project

To add push notifications to the Android project, it is required to add Firebase to the project:

  • Open project in the Android Studio
  • Select Tools, then Firebase from the top menu. Assistant window will open
  • Expand the Cloud Messaging item and click on the Set up Firebase Cloud Messaging
  • Click the Connect to Firebase button. Browser windows will open where you will be asked to login into the Firebase with your Google account. After logging in, the “Connect to Firebase” window will be displayed in the Android Studio.
  • Follow the wizard to add Firebase support into the project
  • Click the Add FCM to your app button and then click on the Accept Changes button to modify your project.

Provide token to the Voximplant cloud

  • After each successful login into Voximplant cloud application should get a push notification token from Firebase and provide it to the Voximplant cloud:
    1. String token = FirebaseInstanceId.getInstance().getToken();
    2. // 'client' is an instance of VoxImplantClient
    3. client.registerForPushNotifications(token);
  • The application should also provide a token if Firebase refreshes it. Implement Android Service that extends FirebaseInstanceIDService, override the onTokenRefresh method of that service and in that method do same token manipulation as described above. Add the service to the Android application manifest so the service is auto-started on application start
  • Handle push notifications

  • Implement second Android Service that extends FirebaseMessagingService. Add the service to the Android application manifest so the service is auto-started on application start
  • Override the onMessageReceived method of that service
  • Inside the onMessageReceived method call, get a message payload via getData and check, that it contains the “voximplant” signature
    1. @Override
    2. public void onMessageReceived(RemoteMessage remoteMessage) {
    3. Map<String, String> message = remoteMessage.getData();
    4. if (message.containsKey("voximplant")) {
    5. // 'client' is an instance of VoxImplantClient. This call
    6. // notifies Voximplant cloud that notification is successfully received.
    7. client.handlePushNotification(message);
    8. }
    9. }
  • After the message with “voximplant” signature is received, if the application is not connected or logged in to the Voximplant cloud, the application should login (and reconnect, if needed) in order to handle an incoming call. It is strongly recommended to disable connectivity check when using Voximplant connect while handling push notifications since it increases the connect time and application may not meet the time requirements to react on push notification.

Add certificates to the Voximplant cloud

  1. Login into the Firebase console with your Google account you have connected early in this tutorial
  2. In the Recent Projects section click on the project that was previously added to Firebase during this tutorial
  3. Click on the Android application name and select Settings
  4. Select the Cloud Messaging tab from the top navigation bar
  5. Copy Sender ID and Server key strings
  6. Login into the Voximplant control panel
  7. Select Settings, Mobile Push Certificates from the top menu
  8. Click on the Add Certificate
  9. In the Certificate for list select the GOOGLE
  10. Enter the Sender ID and Server key strings copied from the Firebase
  11. Click on the Add button
  12. Click on the Certificates List, then on the Bind app button for the certificate just added
  13. In the Bind certificate to application window select the Voximplant application that will issue the calls and click on the Save button

Modify VoxEngine scenario

In order for Voximplant cloud to send push notification to applications, the JavaScript scenario that initiates a call should include a push notification helper:

require(Modules.PushService)

Push notifications are sent automatically

Just use a callUser method from the VoxEngine scenario and push notification will be automatically sent to the app so the app can wake up, connect to Voximplant, login and accept the call.

Use tokens or keys for better security

Voximplant support 3 ways to login from the SDK: with a username and a password, with a username and a token or with a one-time login key. Saving the password is not secure, so we would recommend to use “token” or “ke”y approach. The token is received from an event after the successful login, can be saved and used later instead of a password. Please refer to the tutorial for details. The “login key” approach allows to completely exclude password usage on client-side by offloading it to your backend. Please refer to the tutorial for details.

Comments