在没有推送通知的情况下更新 iOS 徽章
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4861131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating iOS badge without push notifications
提问by fabian789
I have seen a few todo apps that update their app badges at midnight, always showing the correct number of due tasks. They do this withoutthe use of Push Notifications - so my question is: how do they do this? Do they use local notifications - if so, do these get called when the device is turned off? I'm a little confused and would appreciate some input.
我见过一些待办事项应用程序在午夜更新他们的应用程序徽章,总是显示正确的到期任务数量。他们在不使用推送通知的情况下做到这一点- 所以我的问题是:他们是如何做到这一点的?他们是否使用本地通知——如果是这样,当设备关闭时这些通知会被调用吗?我有点困惑,希望得到一些意见。
回答by raaz
Try this
尝试这个
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
To do this through local notifications you have to set the value in applicationIconBadgeNumber
要通过本地通知执行此操作,您必须在 applicationIconBadgeNumber
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = 1;// set here the value of badge
回答by Jiri Trecak
And for everyone using new and shiny Swift:
对于使用全新闪亮 Swift 的每个人:
UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber
Swift 3:
斯威夫特 3:
UIApplication.shared.applicationIconBadgeNumber = someNumber
回答by JustSid
Since iOS 4.0 you can fire local notifications on all devices that run at least iOS 4.0. Look into the UILocalNotification
class, it allows you to set the badge at midnight without having your app running.
从 iOS 4.0 开始,您可以在至少运行 iOS 4.0 的所有设备上触发本地通知。查看UILocalNotification
课程,它允许您在午夜设置徽章而无需运行您的应用程序。
回答by Vladimir
Set UIApplication
's applicationIconBadgeNumber
property in your code when application is running:
在应用程序运行时在代码中设置UIApplication
的applicationIconBadgeNumber
属性:
[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;
回答by Vaibhav Shiledar
For Objective C you have to use:
对于目标 C,您必须使用:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];