sl2883 / richpushonly

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

  • Start with a basic project with CleverTap set up using the steps mentioned here
    • Get the SDK
    • Update the Info.plist with id and token
  • Integrate basic CT
CleverTap.setDebugLevel(CleverTapLogLevel.debug.rawValue)
CleverTap.autoIntegrate();
  • Enable Push notifications (basic baground and killed state)
    • Register for push - ref
    • Ensure that Push notifications and background mode are added to the capabilities
  • Add the notification service extension
    • By just sending a push now, with mutable-content flag checked, the breakpoint should hit the receive function of the extension
  • Add CTNotificationService in the pod file for the extension - ref
    • pod 'CTNotificationService' and run pod install
    • Ensure that CT token, id are in Info.plist of the extension
    • Extend NotificationService from CTNotificationServiceExtension
    • Update the didReceive function as follows
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

CleverTap.setDebugLevel(3)
CleverTap.sharedInstance()?.recordNotificationViewedEvent(withData: request.content.userInfo)
super.didReceive(request, withContentHandler: contentHandler)

Leave the info.plist as it - Keep $(PRODUCT_MODULE_NAME).NotificationService as value for NSExtensionPrincipalClass. Changing this value to CTNotificationServiceExtension stops the debugger from hitting the didReceive function of NotificationService class [This needs to be checked further]

At this point, the images should start working if

  • From CleverTap, we're setting the Advanced options -
    • url for the image (Rich media turned on)
    • mutable-content checked

Making Push impressions work

  • Update the Podfile so now our service extension also depends on CleverTap SDK
target 'NotificationServiceExtention'  do
	pod 'CTNotificationService'
	pod "CleverTap-iOS-SDK"
end
  • Update the NotificationService class to raise an event for CleverTap to record notification viewed (Push impression in the backend)
CleverTap.setDebugLevel(3)
CleverTap.sharedInstance()?.recordNotificationViewedEvent(withData: request.content.userInfo)

For push impressions to go into the correct profile

  • Add "App groups" capability to main target as well as notification service target
    • From your App developer account, go to Certificates, Identifiers & Profiles -> Identifiers. Find your app & the app extension identifiers.
    • Select your app identifier, click on the App Groups and Configure the identifier to be part of an app group.
    • This step will require you to generate a new provisioning profile for the app identifier. Update the profile in the Xcode to sign your app.
    • Once the app groups are set, from your login - set the identity to the userdefaults
if let groupUserDefaults = UserDefaults(suiteName: "group.com.sunny.ctios") {
       groupUserDefaults.set(identity, forKey: "identity")
}

And in your didReceive function (NotificationService class), read that value and login user -

if let groupUserDefaults = UserDefaults(suiteName: "group.com.sunny.ctios") {
      if let identity = (groupUserDefaults.object(forKey: "Identity")) as? String {
      let profile: Dictionary<String, Any> = ["Identity": identity]
       print("[Clevertap] Identity" + " logged in to the service extension")
       CleverTap.sharedInstance()?.onUserLogin(profile)
   }
}

Now that the user is logged in, call the notificationViewed event -

CleverTap.sharedInstance()?.recordNotificationViewedEvent(withData: request.content.userInfo)

Also, make sure to call super.didReceive so that the images are rendered correctly.

Few things to keep in mind -

  • Payload needs to have "mutable-content": 1
  • iOS version of Service extension should be less than equal to the iOS version of the targeted device.
  • Restart of the device may be required
  • Apple supports Service extension from iOS 10

About


Languages

Language:Swift 96.9%Language:Ruby 3.1%