简体   繁体   中英

iOS 7 - silent push notifications

I have setup silent push notification and all works fine when app is in foreground or in background.

The problem is when the application is not active/killed (if i have understand well, any application is killed automatically after 30 seconds when it is in background).

My payload is like this

{"aps":{"alert":"test","sound":"bingbong.aiff","badge":33,"content-available":1}}

All works fine but when i receive this push, badge icon is not update (no 33 is appear near the application icon). This is the first problem.

The second problem is that i dont know how to get the notification when the app is killed.

My idea was to call the service if the badge icon was great than 1, in this way i know that there are some notification to download and i can contact the server to get them.

A silent push notification doesn't need the alert, either the sound to be specified, since it's not presented to the user.

The silent notification you will get through application:didReceiveRemoteNotification:fetchCompletionHandler::

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
     //load the new available content and call the completionhandler.
}

The badge should be displayed if the app has the correct permissions. You can register your app to display the badge like this:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];

Anyway you don't have to check the badge number to download new content. You can simply attach your own key-value-pairs to the notification:

{
    "aps": {
         "badge": 33,
         "content-available":1
    },
    "load-data": 1,
    "load-data-id": 12
}

When you receive the notification in the app just check for your param in the userInfo dictionary:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    int loadData = [[userInfo objectForKey:@"load-data"] intValue];
    int loadDataId = [[userInfo objectForKey:@"load-data-id"] intValue];
}

All of this is documented in the Local and Push Notification Programming Guide in the iOS documentation.

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