简体   繁体   English

收到推送消息后,如何更新iOS主屏幕中的通知徽章编号?

[英]How to update notification badge number in iOS's home screen when received push message?

I want to iOS device can auto update the budges number in home screen (app not run). 我希望iOS设备可以在主屏幕上自动更新伙伴编号(应用未运行)。 Currently, I'm using push notification and it's can receive push notifications OK. 目前,我正在使用推送通知,可以接收推送通知。 But the budges number don't auto update (it's can only update after I clicked to view detail push notice). 但是伙伴编号不会自动更新(只有在我单击以查看详细信息推送通知后才可以更新)。 My questions is how to reload budges number when received push message with app not run (background app) - like LINE chat app? 我的问题是,当收到带有应用程序(后台应用程序)的推送消息未运行时(如LINE聊天应用程序),如何重新加载伙伴号码? My function to show budges number in AppDelegate like bellow: 我的功能来显示AppDelegate中的伙伴数,如下所示:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    int Total_budges = [[userInfo objectForKey:@"badge_count"] intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + Total_budges;
}

Thanks all! 谢谢大家!

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo this Method never going to be called when you application is backGround State else in-active state so just You need Manage it's Badge count from back-End side. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo当应用程序为backGround State或处于非活动状态时,永远不会调用此方法,因此只需从后端管理它的Badge计数即可。

That from you APNS PayLoad with Key or badge must be have an Integer value. 带有密钥或徽章的APNS PayLoad的值必须为Integer值。 like 喜欢

badge": 1 // that value (1) must be an Integer else that badge not display.

so When Push notification arrive that Apple manage automatically display that badge icon on you application icon. 因此,当推送通知到达时,Apple会自动在您的应用程序图标上显示该徽章图标。

For Clear count when application open used: 对于使用应用程序打开时的清除计数:

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

Thanks everybody answered me! 谢谢大家回答我! I was retry and now It's run OK! 我已经重试,现在可以运行了! All steps I do: 1- Recount Total budges in server and response to Apple push message 2- Return with Key "badge" 我要做的所有步骤:1-重新计算服务器中的总数,以及对Apple推送消息的响应2-用密钥“徽章”返回

$body['aps'] = array(
                'alert' => $message,
                'badge' => $Total_msg_unread,
                'sound' => 'default'
                );

Before read Chinttu RoxeN Ramani's reply, I set 'badge' like custom key=> it's can't run. 在阅读Chinttu RoxeN Ramani的回复之前,我将'badge'设置为自定义键=>,它无法运行。 Maybe it's my issue! 也许这是我的问题!

3- In Xcode convert budges value to IntValue. 3-在Xcode中,将budged值转换为IntValue。 One again, Thanks everybody! 再次,谢谢大家!

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

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