简体   繁体   中英

Catch push notification text on lock screen

How can I get the push notification text when the app does not run or in lock screen?

I did try:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    println(userInfo)
    // or just
    println("something")
}

But it doesn't print me anything. Is it wrong function???

didReceiveRemoteNotification function only called this condition.

  1. while app will running and app in foreground.(alert not showing)
  2. if app in background and click notification on home screen

if app not run in device didReceiveRemoteNotification function not called, in this case user click notification in home screen, we identify app launched by notification or not in didFinishLaunchingWithOptions

UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

you get userinfo data in localNotif.userinfo

Try in Swift

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 var userinfo : NSDictionary =launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
        print(launchOptions);
        return true
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM