如何增加重复本地通知的应用程序徽章编号 (iPhone)
我已经设置了每分钟重复一次的本地通知,但是我需要每次都增加应用程序徽章编号。当我现在运行它时,它似乎没有增加,只是保持为 1。请问有人可以帮助我吗?
以下是我创建通知的方法:
// Create the UILocalNotification
UILocalNotification *myNotification = [[UILocalNotification alloc] init];
myNotification.alertBody = @"Blah blah blah...";
myNotification.alertAction = @"Blah";
myNotification.soundName = UILocalNotificationDefaultSoundName;
myNotification.applicationIconBadgeNumber++;
myNotification.timeZone = [NSTimeZone defaultTimeZone];
myNotification.repeatInterval = NSMinuteCalendarUnit;
myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:myNotification];
I've setup a local notification that repeats every minute, however I need the application badge number to increment each time. When I run it at the moment it doesn't seem to increase, it just stays a 1. Please can someone help me out?
Here is how I create the notifications:
// Create the UILocalNotification
UILocalNotification *myNotification = [[UILocalNotification alloc] init];
myNotification.alertBody = @"Blah blah blah...";
myNotification.alertAction = @"Blah";
myNotification.soundName = UILocalNotificationDefaultSoundName;
myNotification.applicationIconBadgeNumber++;
myNotification.timeZone = [NSTimeZone defaultTimeZone];
myNotification.repeatInterval = NSMinuteCalendarUnit;
myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:myNotification];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
经过大量研究后,我发现解决方案是没有解决方案:
iPhone:通过本地通知增加应用程序徽章
当您的应用程序处于后台时,无法使用本地通知动态更新徽章编号。您必须使用推送通知。
After doing lot's of research I figured out the solution is that there is no solution:
iPhone: Incrementing the application badge through a local notification
It is not possible to update dynamically the badge number with local notifications while your app is in the background. You have to use push notifications.
如果您使用外部服务(例如 Parse for Push),这应该很容易完成。只需在触发本地通知时增加解析徽章编号即可。虽然,这是一个特殊情况。
If you use an outside service such as Parse for Push, this should be easily done. Just increment Parses badge number when a local notification is fired. Although, this is a special case.
虽然没有简单的
applicationIconBadgeNumber++
方法,但正如 BFar 提到的,您可以通过在添加或删除通知时更新所有计划的 UILocalNotifications 的 applicationIconBadgeNumbers 来实现您的要求。虽然如果您有使用
repeatInterval
的通知,只要您调用scheduleNotification
和decrementBadgeNumber
,这将不起作用在适当的时候,下面的课程应该能起到作用。While there's no simple
applicationIconBadgeNumber++
method, as BFar mentioned, you can achieve what you're asking by updating all of the scheduled UILocalNotifications' applicationIconBadgeNumbers whenever a notification is added or removed.While this won't work if you have notices that use
repeatInterval
, as long as you callscheduleNotification
anddecrementBadgeNumber
at the right times, the class below should do the trick.我能够使用以下行来完成此操作,同时安排本地通知
并在应用程序委托的另一端
I was able to do it using the following line while schedule the local notification
and on the other end in the appdelegate
尝试类似的方法:
Try something like:
这应该有效。
This should work.
试试这个...它在简单的场景中对我有用...
并且不要忘记在应用程序启动时将徽章图标设置回 0。
Try this ... it worked for me in simple scenario ...
And don't forget to set badge icon back to 0 when app launch.