Configure Mobile App For Push

In order for Growlytics to handle push notifications, following configuration/changes needs to be done in your mobile app:

Step 1: Add/Configure App Target

  1. Turn ON App Groups in for your app target and enable one of the App group ids, in case if you don't have an App Group ID then create one. The name of your app group should be group.{your_bundle_id}.Growlytics.

  2. Turn ON Background mode and set/enable Remote Notification.

  3. Turn ON the Push Notifications capability for your app.

Step2: Add UserNotifications framework

In the App's Target add UserNotifications framework in Linked Frameworks and Libraries and set it to Do not embed.

Step2: Registering for Push notification

Make sure that class, where UserNotificationCenter delegate methods are implemented, should agree to UNUserNotificationCenterDelegate , also set the UNUserNotificationCenterDelegate after the app launch in application:DidFinishLaunchingWithOptions: as shown below.

import UIKit
import Growlytics
import UserNotifications

@QUIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: 
        [UIApplication.LaunchOptionsKey: Any]?) Bool {
        
        // Request for notification permission.
        // With this code, notificaiton permission popup will be shown at app launch.
        // If you don't want to show popup at app launch, move this code block to where you want to request for push permissions.
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
        application.registerForRemoteNotifications()

        // Register Growlytics SDK Callback Handler.
        LifeCycleCallback.shared.register(application: application)
        
        return true
    }
}

Step3: Configure AppDelegate swizzling in SDK

In Growlytics SDK we have swizzled the AppDelegate Class to get all the callbacks related to Push Notifications and also we have applied method swizzling for UserNotificationCenter delegate methods.

In case you prefer not to use swizzling, you can disable the same by adding the flag GROWLYTICS_SWIZZLING in the app’s Info.plist file and setting it to bool value NO.

Below are the callbacks the app would receive on receiving the push notifications. In case you have disabled swizzling, include calls to Growlytics SDK methods on receiving notification callbacks as shown below:

import UIKit
import Growlytics
import UserNotifications

@QUIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
   
     //
     // ....OTHER FUNCTIONS......    
     //

    // MARK:- UserNotifications Framework callback method
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
        //Call only if GROWLYTICS_SWIZZLING is NO 
        GrowlyticsMessagingService.shared.didReceiveNotificationResponse(center, didReceive: response)
    
        //Custom Handling of notification if Any
        let pushDictionary = response.notification.request.content.userInfo print(pushDictionary)
        print(pushDictionary)

        completionHandler()
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        //This is to only to display Alert and enable notification sound completionHandler([.sound,.alert])
    }

}

Step 4: Report Push Token to Growlytics

When you ask for push permission and push permission is approved, an push token is generated and returned by iOS. This push token will be used by Growlytics to send notifications from Growlytics dashboard. To report generated push notification to Growlytics, you can use setPushToken method of GrowlyticsMessagingService class as shown in code snippet below.

import UIKit
import Growlytics
import UserNotifications

@QUIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, NotificatioDataSendingDelegateProtocol {

    //
    // ....OTHER FUNCTIONS......    
    //

    // THIS FUNCTION WILL BE CALLED WHEN PUSH PERMISSION IS GRANTED.        
    func application(_ application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        GrowlyticsMessagingService.shared.setPushToken(token)
        GrowlyticsMessagingService.shared.delegate = self 
    }

    // THIS FUNCTION WILL BE CALLED WHEN PUSH PERMISSION IS DENIED.
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        GrowlyticsMessagingService.shared.didFailToRegisterForPush() 
    }
    
    // THIS FUNCTION WILL BE CALLED BY GROWLYTICS SDK WHEN
    // USER CLICKS ON PUSH NOTIFICATION SENT WITH GROWLYTICS TO PROVIDE PUSH CONTEXT DATA LIKE KEY VALUE PAIRS AND SCREEN NAME.
    func sendNotificationDataToAppDelegate(kvPair: [[String : Any]], 
        screenName: String, action: String) { 
        
    }

}

Step 5: Configure Notification Service Extension

Notifications have got a complete revamp after the release of iOS10 with the introduction of new UserNotifications and UserNotificationsUI framework. And with this we got Notification Service App Extensions, which can be used for following:

  1. Add media support in Notifications: Post iOS10 Apple has given us the ability to add image files to the notifications and this can be done using the Notification Service Extension.

  2. For Tracking Notification Impression: We can track if a Notification is received by the device using the Notification Service Extension.

Follow the below steps to set up Notification Service Extension:

  1. Create a Notification Service Extension Target:

  2. Set the name of the extension target and the programing language which you want to use:

  3. After the target is created, Activate the scheme for Extension when prompted for the same. After this, your extension will be added to the project you will see a class with the extension name provided by you while creating and .plist file associated with it.

  4. Then make sure that the Push Notifications Capability is enabled for the Notification Service Extension created:

  5. Next, you will need to add "UserNotifications.framework" to the extention target that we created. Add UserNotifications framework to Linked Frameworks and Libraries of notification service extension target as shown below:

In the new class created by notification service extension, do the following changes

import UserNotifications 
import Growlytics

class NotificationService: UNNotificationServiceExtension {
    
    var contentHandler: ((UNNotificationContent) -> Void)? 
    var bestAttemptContent: UNMutableNotificationContent?
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        GrwNotificationServiceExtension.didReceive(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.
        
    }
}

Last updated