简体   繁体   中英

How to set badge icon when push notification is received

I'm working on an iOS chat application which is in a UIWebView . I have implemented push notifications as well. But I want to know how to detect if a chat message(push notification) has been received on a device and be able to set a badge icon accordingly.

Using parse.com (default for Push Notifications) you can "send a badge" by setting the "badge" key to whatever number you want. The operating system on the receiving end will then set the badge number for you. See below:

    PFPush *push = [[PFPush alloc] init];
    NSString *userChannel = [NSString stringWithFormat:@"USER-%@", userId];
    [push setChannel:userChannel];
    NSDictionary *data = @{@"alert":@"You have a new review!",@"badge":@1, @"sound":@"default"};
    [push setData:data];
    [push sendPushInBackground];

As of iOS 8, note you must also include the "sound" key if you want the receiver to hear a chime upon reception of push notification.

On the receiving end, you will likely want to clear the badge icon once the user has opened the app. You do that as follows:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [PFPush handlePush:userInfo];
}

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