Skip to content

Instantly share code, notes, and snippets.

@cyrilchandelier
Created April 22, 2018 03:44
Show Gist options
  • Save cyrilchandelier/ba2c3e61dd26e9864eec2f96818de84e to your computer and use it in GitHub Desktop.
Save cyrilchandelier/ba2c3e61dd26e9864eec2f96818de84e to your computer and use it in GitHub Desktop.
Detecting organic vs. local notification vs. remote notification app launches
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)
}
}
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