简体   繁体   English

收到推送通知时如何设置徽章图标

[英]How to set badge icon when push notification is received

I'm working on an iOS chat application which is in a UIWebView . 我工作在iOS聊天应用程序是在一个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. 使用parse.com(“推送通知”的默认设置),您可以通过将“徽章”键设置为所需的任何数字来“发送徽章”。 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. 从iOS 8开始,请注意,如果希望接收方在接收到推送通知时听到提示音,则还必须包括“声音”键。

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM