Using Push Service

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

Optional parameters:

  • userAgent - Optional default user agent used for each request

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

Optional parameters:

  • userAgent - Optional default user agent used for each request

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) {
                it.onSuccess {
                    // push notification registered
                }.onFailure {
                    // 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:

// Overridden 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 Jan 03, 2023 (13:44) Edit on Github Send Feedback
Search

1.5.x

Mobile Token SDK for Android