Created
April 22, 2018 03:44
-
-
Save cyrilchandelier/ba2c3e61dd26e9864eec2f96818de84e to your computer and use it in GitHub Desktop.
Detecting organic vs. local notification vs. remote notification app launches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func applicationDidBecomeActive(_ application: UIApplication) { | |
let notificationController = NotificationController.shared | |
// Track app launch if the notificationController didn't already do it | |
if !notificationController.handledLaunchAnalytics { | |
Analytics.trackAppLaunched(source: .Organic) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private(set) var handledLaunchAnalytics: Bool = false | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
let atAppLaunch: Bool = (UIApplication.shared.applicationState != .active) | |
let request: UNNotificationRequest = response.notification.request | |
if let trigger = request.trigger, trigger is UNPushNotificationTrigger { | |
// if atAppLaunch == true, call Analytics.trackAppLaunched(source: .remoteNotification) | |
// and flag handledLaunchAnalytics as true | |
// then you can handle your remote notification here | |
} else { | |
// if atAppLaunch == true, call Analytics.trackAppLaunched(source: .localNotification) | |
// and flag handledLaunchAnalytics as true | |
// then you can handle your local notification here | |
} | |
completionHandler() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment