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

前端 未结 4 529
醉梦人生
醉梦人生 2021-01-24 06:52

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

相关标签:
4条回答
  • 2021-01-24 07:04

    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.

    0 讨论(0)
  • 2021-01-24 07:14

    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;
    }
    
    0 讨论(0)
  • 2021-01-24 07:20

    Try this in your app delegate:

             - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    
    0 讨论(0)
  • 2021-01-24 07:22

    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.

    0 讨论(0)
提交回复
热议问题