v4.x Integration Guide - phunware/maas-engagement-ios-sdk GitHub Wiki

Requirements

iOS 10 or greater

Xcode 8 or greater

Step 1: Installation

The recommended way to use PWEngagement is via CocoaPods.

Add the following pod to your Podfile: pod 'PWEngagement', '4.0.0'.

Make your project workspace by executing pod install from Terminal.

NOTE: It's required to enable CoreData in your project.

Step 2: p12 Push Notification Certificate

Follow Step 2 at developer.phunware.com

Step 3: Application Setup

Follow Step 3 at developer.phunware.com

Step 4: Application Initialization

  1. Import Engagement framework into your application delegate file by adding import PWEngagement.

  2. Configure it by adding the following code snippet in the application(_: willFinishLaunchingWithOptions:) method.

PWEngagement.configure(with: appId, accessKey: accessKey, signatureKey: signatureKey)

// 
// The below is not required, you move it to anywhere as you want
// This's just an example of requesting user notification authorization
//
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        guard granted else {
            return
        }

        UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings) in
            if settings.authorizationStatus != .authorized {
                UIApplication.shared.registerForRemoteNotifications()
            }
        })
    }

// Handle incoming notifications and notification-related actions.
UNUserNotificationCenter.current().delegate = self
  1. Update ANPS token by adding the following code snippet in the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method
PWEngagement.apnsToken = deviceToken
  1. Handle remote notification by adding the following code snippet in the application(_: didReceiveRemoteNotification: fetchCompletionHandler) method.
PWEngagement.retrieveMessage(with: userInfo) { (message, error) in
    // You may add code here to do what you want

    completionHandler(.newData)
}
  1. Handle user notification from UNUserNotificationCenterDelegate's callback methods.

Message deep linking and displaying incoming message when the app is running in the foreground.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    PWEngagement.retrieveMessage(with: notification.request.content.userInfo) { [weak self] (message, error) in
        // TODO... you may need add code here to show the notification when app is running in the foreground.
        
        // Complete the handler
        completionHandler(.alert)
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    guard let userInfo = response.notification.request.content.userInfo as? [String : Any] else {
        return
    }
    
    PWEngagement.retrieveMessage(with: userInfo) { [weak self] (message, error) in
        // TODO... you may need add deep linking implementation here
    }
    completionHandler()
}