Skip to content

How to integrate Infobip SDK when you need to use another push provider

Ivan Krešić edited this page Sep 1, 2023 · 1 revision

Notice

We don't recommend having multiple push notification providers functioning within the same application because they might collide and interfere with each other.
Please follow Quick start guide before starting implementation of these changes.

In case you still need to have another push provider inside your application, there are some things in the default integration of the MobileMessaging SDK that could be adapted to let both push providers work properly:

Registering for push notifications

MobileMessaging SDK by default registers for remote notifications during MobileMessaging.start() procedure. It is possible to disable this default behaviour starting SDK as follows:

   MobileMessaging
      .withApplicationCode("your application code", notificationType: MMUserNotificationType(options: [.alert, .sound]))?
      .withoutRegisteringForRemoteNotifications()
      .start()

expand to see Objective-C code

    [[[MobileMessaging withApplicationCode: <#your application code#> notificationType: <#your preferred MMUserNotificationType#>]
        withoutRegisteringForRemoteNotifications]
        start:nil];

Notice

Don't forget to register for Push Notifications explicitly by calling MobileMessaging.registerForRemoteNotifications(), if another provider doesn't do it.

Unregistering from push notifications

MobileMessaging SDK by default contains logic to unregister from Remote Notifications in certain cases: when depersonalization transitions to the pending state; when you explicitly stop MobileMessaging. It is possible to disable this default behavior. This might be needed in case if another push provider having different unregistering logic.

   MobileMessaging
      .withApplicationCode("your application code", notificationType: MMUserNotificationType(options: [.alert, .sound]))?
      .withoutUnregisteringForRemoteNotifications()
      .start()

expand to see Objective-C code

    [[[MobileMessaging withApplicationCode: <#your application code#> notificationType: <#your preferred MMUserNotificationType#>]
        withoutUnregisteringForRemoteNotifications]
        start:nil];

Providing device token

Usually device token is provided to the MobileMessaging SDK by calling MobileMessaging.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken) method from AppDelegate's method application:didRegisterForRemoteNotificationsWithDeviceToken:, in case if you are using another push provider there might be different cases where you receive the device token, for proper work of MobileMessaging SDK it needs device token to be provided as soon as you get it and without any changes, the best approach is to do it from AppDelegate's method:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
MobileMessaging.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
 // other push providers might have their code here and handle a deviceToken as well
}

expand to see Objective-C code

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [MobileMessaging didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    // other push providers might have their code here and handle a deviceToken as well
}

Handling of the received push notifications

In order to handle push notifications properly via MobileMessaging SDK and send notification's delivery reports to Infobip MobileMessaging.didReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler) method needs to be called from AppDelegate's method application:didReceiveRemoteNotification:fetchCompletionHandler:, in case of another push provider usage be careful with completionHandler, it needs to be called only once, to achieve that you can use MM_MTMessage.isCorrectPayload method to check that message belongs to MobileMessaging SDK:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
     
     if MM_MTMessage.isCorrectPayload(userInfo) {
         MobileMessaging.didReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler)
     } else {
         // other push vendors might have their code here and handle a remove notification as well. completionHandler needs to be called only once
         completionHandler(.noData)
     }
 }

expand to see Objective-C code

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
   if ([MM_MTMessage isCorrectPayload:userInfo]) {
         [MobileMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
   } else {
         // other push vendors might have their code here and handle a remove notification as well. completionHandler needs to be called only once
         [MobileMessaging completionHandler:noData]
     }
}

UNNotificationCenterDelegate

Other push notifications provider might need to setup UNUserNotificationCenterDelegate, in the case if that functionality is more preferable than MobileMessaging SDK, you may use withoutOverridingNotificationCenterDelegate fabric method to disable a MobileMessaging's default UNUserNotificationCenterDelegate implementation. However, implementing of the UNUserNotificationCenterDelegate by MobileMessaging SDK is required to handle user-selected actions from notifications, and to process notifications that arrive when your app is running in the foreground. (More details here https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate)

 MobileMessaging
     .withApplicationCode("your application code", notificationType: MMUserNotificationType(options: [.alert, .sound]))?
     .withoutOverridingNotificationCenterDelegate()
     .start()

expand to see Objective-C code

    [[[MobileMessaging withApplicationCode: <#your application code#> notificationType: <#your preferred MMUserNotificationType#>]
        withoutOverridingNotificationCenterDelegate]
        start:nil];

Setting up Notification Service Extension

In order to properly handle messages use MM_MTMessage.isCorrectPayload method to check that message belongs to MobileMessaging SDK and then call methods to handle the message as usual, be careful with the contentHandler block, it needs to be called only once:

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
     self.contentHandler = contentHandler
     self.originalContent = request.content
     if MM_MTMessage.isCorrectPayload(request.content.userInfo) {
         MobileMessagingNotificationServiceExtension.startWithApplicationCode("<# your application code #>")
         MobileMessaging.logger = MMLumberjackLogger(logOutput: MMLogOutput.Console, logLevel: MMLogLevel.All)
         MobileMessagingNotificationServiceExtension.didReceive(request, withContentHandler: contentHandler)
     } else {
         //handling by another push provider
     }
 }
Clone this wiki locally