我注意到,当在ios设备中收到本地通知时,通知会显示在通知中心,但应用程序徽章编号在应用程序关闭时不会更新.
我需要触摸通知中心中的通知,以便将本地推送消息传输到应用程序.
这是正常行为吗?这可以通过使用远程推送通知来解决吗?
解决方法
您可以在UIlocalnotification对象中使用applicationIconBadgeNumber参数.
基本上:
localnotificationObject.applicationIconBadgeNumber++;
例:
UIlocalnotification *localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20]; localnotification.alertBody = @"Some Alert"; //the following line is important to set badge number localnotification.applicationIconBadgeNumber++; [[UIApplication sharedApplication] schedulelocalnotification:localnotification];
但问题是,徽章编号在后续(多个)本地通知中没有增加(这里有一个场景但是为了简单起见,我们只要说徽章在2个或更多,背靠背,本地通知后仍保持1 ).
在这种情况下,是的…推送通知似乎是要走的路
(但要注意推送通知并不总是可靠的……检查:link)
嗯…要使用推送通知进行正确的徽章编号更新,您应该知道可以在推送通知的有效负载中发送徽章计数.
收到此推送通知后,iOS会将徽章计数更改为推送通知中指定的徽章计数(& app不需要为此打开).
示例(续):
将applicationIconBadgeNumber设置为0,因为它在某些情况下有帮助(可选)
- (void)applicationWillResignActive:(UIApplication *)application { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } - (void)applicationWillTerminate:(UIApplication *)application { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; }
额外:
您还可以在终止/关闭或辞职时手动设置徽章编号.
通常……在以下任何或所有方法中:
> -applicationWillResignActive
> -applicationDidEnterBackground
> -applicationWillTerminate(在应用关闭时设置badgeNumber)
例:
- (void)applicationWillResignActive:(UIApplication *)application { //Called when the application is about to move from active to inactive state. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledlocalnotifications] count]]; //... } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledlocalnotifications] count]]; //... }