简体   繁体   中英

How can I get array of arrived Remote Notifications in iOS when the application open?

I'm currently developing an iOS application that receive push notification from the web server.

I can receive my notification just fine when the application run in the background / foreground, but when the device is receiving notification while my application got terminated (swiped from the multitasking mode or lock the device), the DidReceiveRemoteNotification method doesn't get called (but the notification & banner does appear!).

So I'm thinking about getting all the arrived (unread) notification and clear all of them when the user launch the application (at FinishedLauncing method) and then I can display some of the message first, because fetching the data from the server do takes some time.

I do can get one message from the launchOptions if the user clicked on one of the notification to open my apps. But is there a way to get them all?

This is not possible to get all the notifications you received.

You can only receive payload of notification on which user tapped or selected from the notification center.

But there is a way, using that you can process your every push notification. add key content-available with value 1 in to your aps dictionary.

so it will look like,

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
        },
        "badge" : 5
        "content-available" : 1
    }
}

If iOS system detects pushNotification with this key having value 1 , it will call application:didReceiveRemoteNotification:fetchCompletionHandler: of your appDelegate.

userInfo is the dictionary containing push notifications. aps is the key for push notifications body.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
 NSLog(@"userInfo  %@",userInfo);   

 // you can get the required message as below

 NSString *msg = [userInfo valueForKey:@"aps"];
 NSLog(@"Push Notification:%@",msg);
}

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