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, the service will return an error.
Push Service communicates with the Mobile Token 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 contextbaseURL
- address, where your operations server can be reachedstrategy
- a strategy used when validating HTTPS requests. The 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 contextbaseURL
- address, where your operations server can be reachedhttpClient
-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, callback: (result: Result<Unit>) -> Unit)
- Registers Firebase Cloud Messaging token on the backendregisterHuawei(hmsToken: String, callback: (result: Result<Unit>) -> Unit)
- Registers Huawei Mobile Services token on the backend
Messaging token on the backend
fcmToken
- Firebase Cloud Messaging token.hmsToken
- Huawei Mobile Services tokencallback
- Called when the request finishes.
Registering to Push Notifications
Android (with Google Play Services)
To register an app to push notifications, you can simply call the register
method:
// first, retrieve Firebase token (do so in the background thread)
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.
Huawei (HarmonyOS / EMUI)
For Huawei devices, you can also register your app to receive push notifications using Huawei Push Kit. To integrate Huawei Push Kit into your app, please refer to the Huawei Push Kit documentation.
// first, retrieve HMS token (do so in the background thread)
try {
val appId = AGConnectOptionsBuilder().build(appContext).getString("client/app_id")
val token = HmsInstanceId.getInstance(appContext).getToken(appId, "HCM")
if (token.isNotEmpty()) {
pushService.registerHuawei(token) {
it.onSuccess {
// push notification registered
}.onFailure {
// push notification registration failed
}
} else {
// token retrieval failed
}
} catch (e: Exception) {
// token retrieval failed
}
For more information visit official documentation
Receiving WMT Push Notifications
To process the raw notification obtained from the Firebase Cloud Messaging service (FCM) or from the HUAWEI Mobile Services (HMS), you can use the 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 propertiesid
of the operationname
of the operationoriginalData
- data on which was the push message constructed
PushMessageOperationFinished
- an operation was finished, successfully or non-successfully with the following propertiesid
of the operationname
of the operationresult
of the operation (for example that the operation was canceled by the user).originalData
- data on which was the push message constructed
PushMessageInboxReceived
- a new inbox message was triggered with the idid
of the messageoriginalData
- 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
}
}