每天午夜更新图标徽章

时间:2015-10-08 11:49:51

标签: ios auto-update

我正在创建一个应用程序,显示自两个人建立关系以来的天数。我想在应用图标徽章上显示天数。我知道如何在用户离开应用程序后执行此操作,但是我想每天更新图标徽章,即使应用程序未在后台打开或运行,这样用户也无需打开应用程序即可知道天数。 “BeenTogether”是一个类似的应用程序,并正在做同样的事情,所以我相信它有可能以某种方式。关于我怎么能做到这一点的任何想法?

3 个答案:

答案 0 :(得分:2)

我确信这种情况的变化已经被问了很多次,但答案仍然是否定的。有一些方法可以对它进行近似,但都有缺点。

  • 无声推送通知。当然,这意味着您需要知道他们的时区,他们需要建立网络连接。
  • 后台抓取。这定期运行"因此,您无法在完全午夜更新徽章,但通常会非常接近。

答案 1 :(得分:1)

您可以通过本地通知实现同样的目的。我假设你开始约会时有约会。因此,您可以使用本地通知每天更新徽章计数。

答案 2 :(得分:0)

I am not sure everyone understood my question right, because it turns out to be possible to increment the icon badge by one everyday at midnight, without having to schedule 64 notifications. The solution is rather simple: //Set a random date (only the time matters because it is repeated everyday), but make sure that the time is at midnight!! NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setYear:2015]; [dateComponents setMonth:1]; [dateComponents setDay:1]; [dateComponents setHour:0]; [dateComponents setMinute:0]; //Create an NSDate from the date components NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDate *configuredDate = [calendar dateFromComponents:dateComponents]; //Schedule a local notification, set the repeatInterval to daily UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = configuredDate; localNotification.alertBody = nil; localNotification.alertAction = nil; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitDay; //Add one to the icon badge number localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
相关问题