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 Mobile Push Registration API.
Creating an Instance
On Top of the PowerAuthSDK
instance
import WultraMobileTokenSDK
import WultraPowerAuthNetworking
let networkingConfig = WPNConfig(
baseUrl: URL(string: "https://myservice.com/mtoken/push/api/")!,
sslValidation: .default
)
// powerAuth is instance of PowerAuthSDK
let pushService = powerAuth.createWMTPush(networkingConfig: networkingConfig)
On Top of the WPNNetworkingService
instance
import WultraMobileTokenSDK
// networkingService is instance of WPNNetworkingService
let pushService = networkingService.createWMTPush()
Push Service API Reference
All available methods of the WMTPush
API are:
pushNotificationsRegisteredOnServer
- If there was already made a successful request.acceptLanguage
- Language settings, that will be sent along with each request.registerDeviceTokenForPushNotifications(token: Data, completionHandler: @escaping (_ success: Bool, _ error: WMTError?) -> Void)
- Registers push token on the backend.token
- token data retrieved from APNS.completionHandler
- Called when the request finishes. Always called on the main thread.
Registering to WMT Push Notifications
To register your app to push notifications regarding the operations, you can simply call the registerDeviceTokenForPushNotifications
method:
// UIApplicationDelegate method
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
pushService.registerDeviceTokenForPushNotifications(token: deviceToken) { success, error in
guard success else {
// push registration failed
return
}
}
}
The above method will get called only if you registered the app to receive push notifications. For more information, visit the official documentation.
Receiving WMT Push Notifications
To process the raw notification obtained from the Apple Push Notification Service (APNS), you can use WMTPushParser
helper class that will parse the notification into a WMTPushMessage
result.
The WMTPushMessage
can be following values
operationCreated
- a new operation was triggered with the following parametersid
of the operationname
of the operationcontent
(optional) of the message presented to the user.originalData
- data on which was the push message constructed
operationFinished
- an operation was finished, successfully or non-successfully with the following parametersid
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
inboxMessageReceived
- a new inbox message was triggered.id
of the messageoriginalData
- data on which was the push message constructed
Example push notification processing:
// MARK: - UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if let wmtnf = WMTPushParser.parseNotification(notification) {
// process the mtoken notification and react to it in the UI
} else {
// process all the other notification types using your own logic
}
}
Error handling
Every error produced by the Push Service is of a WMTError
type. For more information see detailed error handling documentation.