简体   繁体   中英

On iOS, how can my app get the notification data that it received while the phone was in the background?

Is there a way in AppDelegate to get a queue of all the notification data? (Once the user opens the app).

As per apple documentation:

When a remote notification arrives, the system calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method. Notifications usually signal the availability of new information. In your app delegate method, you might begin downloading new data from a server so that you can update your app's data structures. You might also use the notification to update your user interface.

You have to enable remote notification background mode , only then you will get a callback to the above mentioned method.

Try this in your app delegate :

         - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
      fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

For UILocalNotification , you can get received notifications by calling following function in your AppDelegate :

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
    // Application was in the background when notification was delivered.
    } 
}

If you want to find the list of local Notifications, that app has already set, check this solution .

You can check for launchOptions Dictionary, for received Notification, when App resumes on tapping a notification,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
  NSLog(@"Launch Options:%@",launchOptions);

  return YES;
}

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