How to Avoid Common Push Notification Issues on iOS Devices Using iOS SDK version 8.x.x?

Problem

Common issues with Push notifications, including Push Campaign impressions not being tracked and images not rendering even though Push notifications are successfully delivered to device with SDK version 8.x.x. 

info

Information

  • To track impressions, ensure that the Notification Service Extension is implemented in your app.
  • Additionally, enable the Show iOS impressions toggle on the MoEngage dashboard by navigating to Settings → Channel → Push → IOS.iOS imp.png

Solution

Perform the following steps:

  1. Add app_group_id to Your Project’s Main Target for Each Configuration:

    You can add the same app_group_id or different ones across configurations.

    info

    Information

    If your configurations use different Bundle Identifiers but share the same app_group_id, issues may arise. Specifically, if two apps with different Bundle Identifiers but the same app_group_id are installed on the same device, the Notification Service Extension may fail.

    To avoid this, you can use the same app_group_id across multiple configurations, but be aware that you won't be able to test all apps simultaneously on a single device.

     

    app group.png

  2. Add app_group_id During SDK Initialization:
        • If you initialize the SDK using info.plist, add the APP_GROUP_ID key inside the MoEngage key in your info.plist file.
        • If you initialize the SDK using sdkConfig, add the APP_GROUP_ID in the sdkConfig variable inside the AppDelegate file.

    app_group_id in info.plist:app id.png

    app_group_id in sdkConfig variable:

    Swift
    var sdkConfig = MOSDKConfig.init(withAppID: "XXXXXXXXXXXXXXXX")
    sdkConfig.moeDataCenter = .data_center_0X
    sdkConfig.appGroupID = "group.com.XXXXXXXXXXXXXXXX"

     

    Objective-C
    MOSDKConfig* sdkConfig = [[MOSDKConfig alloc] initWithAppID: @"XXXXXXXXXXXXXXXX"];
    sdkConfig.moeDataCenter = MODataCenterData_center_0X;
    sdkConfig.appGroupID = @"group.com.XXXXXXXXXXXXXXXX";

     

  3. Create a Notification Service Extension Target:NSE.png
    • Name the Extension Target: Choose a name for the extension target. Swift is recommended as the language as it works seamlessly with Objective-C projects too.name.png
    • Enable Push Notifications: In the extension target's Signing & Capabilities section, enable Push Notifications and add the AppGroupId.enable.png
    • Align the Minimum Deployment iOS Version: Ensure that the minimum deployment iOS version of the Notification Service Extension matches the iOS version of the main app.support.png
  4. Integrate the MORichNotification Framework into the Notification Service Extension Target: In your podfile, add the Notification Service Extension as a separate target and install the MORichNotification framework.
    Swift
    target 'MoEngageDemo' do
      use_frameworks!
      pod 'MoEngage-iOS-SDK'
    end
    
    target 'MoEngageNotificationService' do
      use_frameworks! #use use_frameworks only if included in main target as in above scenario
      pod 'MoEngageRichNotification'
    end
  5. Code changes in NotificationService.swift file:
    Swift
    import UserNotifications
    import MoEngageRichNotification
     
    class NotificationService: UNNotificationServiceExtension {
     
        var contentHandler: ((UNNotificationContent) - Void)?
        var bestAttemptContent: UNMutableNotificationContent?
     
        override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) - Void) {	
          MORichNotification.setAppGroupID("group.com.XXXXXXXXXXXXXXXX")
          self.contentHandler = contentHandler
          bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
          MORichNotification.handle(richNotificationRequest: request, withContentHandler: contentHandler)
        }
        
        override func serviceExtensionTimeWillExpire() {
            // Called just before the extension will be terminated by the system.
            // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
            if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
                contentHandler(bestAttemptContent)
            }
        }
    }

     

  6. Check Build Phases for the Main App Target: In the Build Phases section of the main app target, locate the Embed App Extensions / Embed Foundation Extensions section. Ensure that the Copy only when installing option is unchecked.copy.png
  7. Ensure Consistent appGroupId Across Configurations: Verify that the appGroupId is consistent across all schemes and configurations (e.g. Debug/Release/QA/UAT) in the project. 
  8. Align Build Configuration: When running or archiving the project, make sure that the Build Configuration for both the Main Target and the Notification Service Extension Target points to the same scheme/configuration.run.pngbuild.png

Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?