简体   繁体   English

根据ios中的未读消息更新应用程序图标的徽章计数

[英]Update Badge count of app icon according to unread message in ios

I need the count value of unread as badge number.and badge number of app icon has to reduced and increase according to the unread message count.(increase if new unread message ,reduced if unread message is read) 我需要将未读的计数值作为徽章编号。应用图标的徽章编号必须根据未读消息计数进行减少和增加。(如果是新的未读消息,则增加;如果已读未读消息,则减少)

->[" Unread " is the count of unread messages.] -> [“ Unread ”是未读邮件的数量。]

NSString *unread =[[NSUserDefaults standardUserDefaults]valueForKey:@"unread"];                 
int badge = [unread intValue];
[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];

Usually the badge number is set by the OS when you receive JSON in the form of: 通常,当您以以下形式接收JSON时,徽章编号由操作系统设置:

{
    "aps" : {
        "alert" : "New notification!",
        "badge" : 2
    }
}

So the server sets the badge number, meaning that you have to keep track of how many notifications a user has. 因此,服务器会设置徽章编号,这意味着您必须跟踪用户收到的通知数量。

On the client side, you have to clear the notification like this: 在客户端,您必须像这样清除通知:

application.applicationIconBadgeNumber = application.applicationIconBadgeNumber - 1; // Decrement counter

Or you can just set them all to 0 and assume they're all read once app is opened, like this: 或者,您可以将它们全部设置为0并假设一旦打开应用程序就可以全部读取它们,如下所示:

application.applicationIconBadgeNumber = 0;

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

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