[英]Update iOS icon badge number
我有一个图标徽章编号更新要求。 该应用程序跟踪任务。 我希望应用程序有一个徽章,显示每天到期的任务数量。 徽章编号需要更新的情况基本上有两种:
我知道如何处理第二种情况。 我可以在 applicationResignActive 功能中设置徽章编号。 但是,午夜自动更新对我来说是个技巧。 要更新徽章编号,我需要调用应用程序的 func 来计算当天到期的任务。 但是,在午夜,应用程序可能处于所有可能的情况:前台、后台和未运行。 我怎样才能做到这一点? 谢谢你。
======================================
为了更清楚地说明我的要求,我希望徽章编号每天都能正确更新,即使用户从未打开过一整天或连续几天的应用程序。 此外,我会尽量避免服务器端支持,因为到目前为止该应用程序是一个独立的应用程序。 非常感谢任何帮助。
======================================
最后更新:我接受了 Vitaliy 的回答。 但是,他的回答要求该应用程序每天至少打开一次。 否则,事件将不会触发,并且无法更新徽章编号。
此外,就我而言,每次应用程序进入后台事件触发时,我都必须删除现有通知并安排新通知,并重新计算最新的徽章编号。
我还是有兴趣用一些方法来处理app不是每天都打开的情况,你如何确保徽章号是正确的。 到目前为止,最简单的方法是设置一些服务器并让它定期向应用程序推送通知。
您可以使用UILocalNotification
实现它:
UILocalNotification
与您计算的徽章计数示例代码:
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Calculate nearest midnight or any other date, which you need
NSDate *nearestMidnight = [self nearestMidnight];
// Create and setup local notification
UILocalNotification *notification = [UILocalNotification new];
notification.alertTitle = @"Some title";
notification.alertBody = @"Some message";
notification.fireDate = nearestMidnight;
// Optional set repeat interval, if user didn't launch the app after nearest midnight
notification.repeatInterval = NSCalendarUnitDay;
// Calculate badge count and set it to notification
notification.applicationIconBadgeNumber = [self calculateBadgeCountForDate:nearestMidnight];
[application scheduleLocalNotification:notification];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.