Skip to main content

Push Token

For receiving push notification, we need to set the app device push token via MC using SetPushTokenEvent. This table shows the list of events that are used to set the app device push token.

We use different Push Notification provider depending on the client platform, you the app has to use the correct prefix:

Client platformPush Notification providerPush Token prefix
Android (without Flutter apps)Google's Firebase Cloud Messaging (FCM)FCM:
Flutter on Android, Flutter on iOSGoogle's Firebase Cloud Messaging (FCM)FCMF:
HuaweiHuawei Push Kit (HPK)HPK:
iOS (without Flutter apps)Apple Push Notification service (APNs)APN:

SetPushToken event flow-diagram

This diagram shows the internal workings of the set app device push token functionality.

In order to keep the push token up to date on the server at all times, you could call the SetPushTokenEvent after every successful login. Alternatively to reduce unnecessary communication with your server, you could store a hash of your token, and with each login, compare it with the hash of the current token returned by your Push Notification provider before deciding whether to trigger SetPushTokenEvent.

Push Notifications with TMS Flow diagram

The event flow diagram illustrates the sequence of events during the Push Notification and TMS process for KOBIL products.

iOS/Swift

There are few steps you have to implement for getting your app device push token

  1. Register your app for RemoteNotifications. You can add this to your AppDelegate 
AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

[UIApplication.LaunchOptionsKey: Any]?) -> Bool {

registerForPushNotifications(application: application)

}
RegisterForRemoteNotifications

func registerForPushNotifications(application: UIApplication){

if #available(iOS 10.0, \*){

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(

options: [.badge, .sound, .alert], completionHandler: { (granted, error) in

DispatchQueue.main.async {

UIApplication.shared.registerForRemoteNotifications()

}

})

}

else {

let notificationSettings = UIUserNotificationSettings(types:

[.badge, .sound, .alert], categories: nil)

application.registerUserNotificationSettings(notificationSettings)

}
}

You must save the app device token with the prefix "APN:". If you do not provide any prefix the master controller will append "GCM:", and this will lead to an error and the application will not receive the push notification.  So, this is important to save the app device token with the server type which you are using for push notification. Here we are using APNS, so we have added "APN:" as a prefix.

RegisterForRemoteNotificationsWithDeviceToken

func application(_ application: UIApplication,

didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

//save app device token

let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

if deviceTokenString.isEmpty == false {

GlobalConstant.const.pushToken = "APN:\(deviceTokenString)"

}else {

//Set hardcoded string but not expect you received push on device with this token.

GlobalConstant.const.pushToken = "Axjdp2345mnofKHlpf1234bdFh05"

}

}

Also, do not forget to add the push notification in signing capabilities as shown in the below screenshot

After getting the app device token, save it for later uses. Once you received LoginResultEvent and its status is OK trigger the setPushToken event.

SetPushToken

func setPushToken() {

//Create event

let event = KSMSetPushTokenEvent(pushToken: GlobalConstant.const.pushToken)

MasterControllerAdapter.sharedInstance.sendEvent2MasterController(event) { (result) in

guard let resultEvent = result as? KSMSetPushTokenResultEvent else {

return

}

switch (resultEvent.status){

// To do handle this status accordingly.

}

}

}

Android/Kotlin (also Huawei), Flutter

For generating app device push token using FCM, we need to have certain configuration. Google gives a good description where you can find all the FCM configuration related steps.

For generating app device push token using Huawei Push Kit (HPK), we need to have certain configuration. Huawei gives a detailed description where you can find all the HMS configuration related steps.

Once you get the app device push token either using FCM or HMS, we need to send the same to MC using SetPushTokenEvent and as a result of the same event, we receive SetPushTokenResultEvent. We have created a dedicated method in the MCHandler class of our example app to trigger the respective event. Below is the code snippet:

    fun triggerSetPushToken(pushToken: String) {
mcEventHandler?.postEvent(SetPushTokenEvent(pushToken))?.then {
// handle result
// this is a good place to trigger SetLocalesEvent
}
}

After Generating the app device push tokens you will need to set up the push environment on ssms so that you can receive push notifications from the server.