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.
Turn ON Background mode and set/enable Remote Notification.
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.
importUIKitimportGrowlyticsimportUserNotifications@QUIApplicationMainclassAppDelegate:UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {var window: UIWindow?funcapplication(_application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [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 = selflet authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _in }) application.registerForRemoteNotifications()// Register Growlytics SDK Callback Handler. LifeCycleCallback.shared.register(application: application)returntrue }}
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:
importUIKitimportGrowlyticsimportUserNotifications@QUIApplicationMainclassAppDelegate:UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {//// ....OTHER FUNCTIONS...... //// MARK:- UserNotifications Framework callback method@available(iOS10.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 Anylet pushDictionary = response.notification.request.content.userInfo print(pushDictionary)print(pushDictionary)completionHandler() }@available(iOS10.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.
importUIKitimportGrowlyticsimportUserNotifications@QUIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, NotificatioDataSendingDelegateProtocol {
//// ....OTHER FUNCTIONS...... //// THIS FUNCTION WILL BE CALLED WHEN PUSH PERMISSION IS GRANTED. funcapplication(_application: UIApplication, didRegisterForRemoteNotificationsWithDeviceTokendeviceToken: Data) { GrowlyticsMessagingService.shared.setPushToken(token) GrowlyticsMessagingService.shared.delegate = self }// THIS FUNCTION WILL BE CALLED WHEN PUSH PERMISSION IS DENIED.funcapplication(_application: UIApplication, didFailToRegisterForRemoteNotificationsWithErrorerror: 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.
funcsendNotificationDataToAppDelegate(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:
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.
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:
Create a Notification Service Extension Target:
Set the name of the extension target and the programing language which you want to use:
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.
Then make sure that the Push Notifications Capability is enabled for the Notification Service Extension created:
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
importUserNotificationsimportGrowlyticsclassNotificationService: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) }overridefuncserviceExtensionTimeWillExpire() {// 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. }}