Using Push Service

Introduction

Push Service is responsible for registering the device for the push notifications about the Operations that are tied to the current PowerAuth activation.

Note: Before using Push Service, you need to have a PowerAuthSDK object available and initialized with a valid activation. Without a valid PowerAuth activation, service will return an error.

Push Service communicates with Mobile Push Registration API.

Creating an Instance

Extension Factory With SSL Validation Strategy

This factory method will create its own OkHttpClient instance based on the chosen SSL validation strategy.

fun PowerAuthSDK.createPushService(appContext: Context, baseURL: String, strategy: SSLValidationStrategy): IPushService
  • appContext - application context
  • baseURL - address, where your operations server can be reached
  • strategy - strategy used when validating HTTPS requests. Following strategies can be used:
    • SSLValidationStrategy.default
    • SSLValidationStrategy.noValidation
    • SSLValidationStrategy.sslPinning

Extension Factory With OkHttpClient

fun PowerAuthSDK.createPushService(appContext: Context, baseURL: String, httpClient: OkHttpClient): IPushService
  • appContext - application context
  • baseURL - address, where your operations server can be reached
  • httpClient - OkHttpClient instance used for API requests

Push Service API Reference

All available methods of the IPushService API are:

  • acceptLanguage - Language settings, that will be sent along with each request.
  • register(fcmToken: String, listener: IPushRegisterListener) - Registers Firebase Cloud Messaging token on the backend
    • fcmToken - Firebase Cloud Messaging token.
    • listener - Called when the request finishes.

Registering to Push Notifications

To register an app to push notifications, you can simply call the register method:

// first, retrieve FireBase token
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        task.result?.token?.let { token ->
            pushService.register(token, object: IPushRegisterListener {
                override fun onSuccess() {
                    // push notification registered
                }

                override fun onFailure(e: ApiError) {
                    // push notification registration failed
                }
            })
        }       
    } else {
        // on error
    }
}

To be able to successfully process notifications, you need to register the app to receive push notifications in the first place. For more information visit official documentation.

Receiving WMT Push Notifications

To process the raw notification obtained from Firebase Cloud Messaging service (FCM), you can use PushParser helper class that will parse the notification into a PushMessage result.

The PushMessage is an abstract class that is implemented by following classes for concrete results

  • PushMessageOperationCreated - a new operation was triggered with the following properties
    • id of the operation
    • name of the operation
    • originalData - data on which was the push message constructed
  • PushMessageOperationFinished - an operation was finished, successfully or non-successfully with following properties
    • id of the operation
    • name of the operation
    • result of the operation (for example that the operation was canceled by the user).
    • originalData - data on which was the push message constructed

Example push notification processing:

// Overriden method of FirebaseMessagingService
override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    val push = PushParser.parseNotification(remoteMessage.data)
    if (push != null) {
        // process the mtoken notification and react to it in the UI
    } else {
        // process all the other notification types using your own logic
    }
}
Last updated on Nov 04, 2021 (13:13) Edit on Github Send Feedback
Search

2022.04

Mobile Token

Mobile Token SDK for Android