无需推送通知即可更新 iOS 徽章

我见过一些待办事项应用程序在午夜更新它们的应用程序徽章,总是显示正确的到期任务数量。他们这样做 没有的使用推送通知-所以我的问题是: 他们如何做到这一点?他们是否使用本地通知——如果是,当设备关闭时是否调用本地通知?我有点困惑,希望你能帮帮我。

58985 次浏览

Set UIApplication's applicationIconBadgeNumber property in your code when application is running:

[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;

Try this

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

To do this through local notifications you have to set the value in applicationIconBadgeNumber

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = 1;// set here the value of badge

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.

And for everyone using new and shiny Swift:

UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber

Swift 3:

UIApplication.shared.applicationIconBadgeNumber = someNumber

For Objective C you have to use:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];

Swift 5

UIApplication.shared.applicationIconBadgeNumber = someNumber