Skip to main content

Push Token

For receiving push notifications, 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 providers depending on the client platform, so 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.

Implementation Examples

iOS/Swift

There are a 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, it 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 use. Once you receive 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)

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
}
}

Flutter/Dart

For Flutter applications, we use Firebase Cloud Messaging (FCM) for both Android and iOS platforms. The token must be prefixed with "FCMF:" to indicate it's from a Flutter FCM implementation.

NotificationService Setup (Flutter/Dart)
// Call this after successful login
Future<void> setupPushNotifications() async {
// Initialize Firebase Messaging
final fcm = FirebaseMessaging.instance;

// Request permission (especially important for iOS)
final settings = await fcm.requestPermission(
alert: true,
badge: true,
sound: true,
);

if (settings.authorizationStatus == AuthorizationStatus.authorized) {
// Get the FCM token
final token = await fcm.getToken();
if (token != null) {
// Add FCMF prefix for Flutter FCM
final pushToken = 'FCMF:$token';

// Send token to MC after successful login
await sendPushTokenToMC(pushToken);
}
} else {
print('❌ User declined push notification permission');
}
}

Future<void> sendPushTokenToMC(String pushToken) async {
try {
// Create SetPushTokenEvent
final setPushTokenEvent = SetPushTokenEventT()
..pushToken = pushToken;

// Get your McWrapperApi instance
final mcApi = locator<McWrapperApi>();

// Send the event
final response = await mcApi.send(setPushTokenEvent);

response.fold(
(error) => print("❌ Push token error: $error"),
(result) => print("✅ Push token sent successfully"),
);

} catch (e) {
print("❌ Exception setting push token: $e");
}
}