iOS徽章编号实时更新 [英] iOS badge number live update

查看:89
本文介绍了iOS徽章编号实时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为iOS创建了一个自定义日历,我正在尝试使用徽章编号来显示当天的编号,但是过了一天后数字没有变化,我的意思是他们应该更新热闹这里是我的代码:我需要类似天气实时应用程序,此应用程序不发送任何推送通知!

I created a custom calendar for iOS , and I am trying using badge number to show number of the Day , but numbers did not change after one day passed , I mean they should update lively here is my code : I need something like weather live application , this application does not send any push notification !

    int a = [DateComponents showDay];
    [UIApplication sharedApplication].applicationIconBadgeNumber = a ;


推荐答案

目前在iOS中执行此操作并不容易。您可以关闭,但不能每天可靠地更新徽章,除非用户偶尔打开应用

It's not easy to do this currently in iOS. You can get close, but not update the badge reliably every day unless the user opens the app occasionally.

您需要使用 UILocalNotification 。您可以创建和安排更新应用徽章的无声通知,而不会提醒用户或播放如下警告声音:

You'll need to use UILocalNotification. You can create and schedule a "silent" notification that updates the app badge without alerting the user or playing an alert sound like this:

UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
notification.timeZone = [NSTimeZone systemTimeZone];
notification.alertBody = nil;
notification.soundName = nil;
notification.applicationIconBadgeNumber = dayTomorrow;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];

其中 dayTomorrow 是明天的int这个月。并且是一段时间间隔 - 这可能是间隔到午夜。发送此通知时,用户将没有警报或声音,但应将应用徽章更新为新值。您可以每天使用 repeatInterval 属性重复此通知,但问题是第二天您需要更改 dayTomorrow 是一个不同的值,在那之后的第二天。但重复警报在 applicationIconBadgeNumber 中始终具有相同的值。

where dayTomorrow is an int of tomorrow's day of the month. and seconds is some time interval - this could be something like the interval to midnight. When this notification is delivered, there will be no alert or sound for the user, but the app badge should be updated to the new value. You could make this notification repeat every day using the repeatInterval property, but the problem here though is that the next day you need to change dayTomorrow to be a different value, and again the day after that. But a repeated alert will always have the same value in applicationIconBadgeNumber.

所以我认为实现任何目标的唯一方法接近你想要的是安排多个本地通知 - 你可以提前安排最多64个 - 每个相隔一天,每个当天的值设置为 applicationIconBadgeNumber 。这些必须是非重复的,因此请确保将 repeatInterval 设置为nil(或者不设置它,因为nil是默认值)。假设所有这些通知都是由iOS可靠地传递的,您可以静默更新徽章编号,而无需在最长64天内打扰用户。之后,徽章将停止更新... 除非您设法在此期间安排更多通知 - 即用户打开应用时。你可以尝试做的是在应用启动时取消所有现有的预定通知:

So I think the only way to achieve anything close to what you want is to schedule multiple of those local notifications - you can schedule up to 64 in advance - each one a day apart, and each one with that day's value set as the applicationIconBadgeNumber. These need to be non-repeating, so make sure you set repeatInterval to nil (or don't set it, as nil is the default value). Assuming all of these notifications are delivered reliably by iOS, you would be able to update the badge number silently and without bothering the user for up to 64 days. After that, the badge would stop updating... unless you manage to schedule more notifications in the meantime - i.e. when the user opens up the app. What you could try doing is cancelling all existing scheduled notifications at app launch:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

然后提前64天循环,安排每天午夜通知,并在正确的一天 applicationIconBadgeNumber 属性的月份。只要用户每64天至少打开一次您的应用,您就会使徽章图标保持最新状态。如果您设想您的应用用户经常打开应用(例如,每天多次),那么优化可能是在取消之前检查已安排的现有通知的数量并创建64个新通知,如下所示:

and then loop through 64 days in advance, scheduling a notification for midnight each day with the correct day of the month for the applicationIconBadgeNumber property. As long as the user opened up your app at least once every 64 days, you would keep the badge icon up to date. If you envisage your app users opening the app frequently (e.g. multiple times per day), then an optimisation might be to check the number of existing notifications scheduled before cancelling them and creating 64 new ones, like this:

if ([[[UIApplication sharedApplication] scheduledLocalNotifications] count] < 10)
{
    //there are 10 or fewer days' worth of notifications scheduled, so create and
    //schedule more here, up to 64 in total.
}

还有几点需要注意:


  • 我不确定本地通知的可靠性。推送通知无法保证,但我希望并期望保证提供本地通知。例如如果手机在午夜关闭并且仅在早上7点开启,我希望安排在午夜的本地通知会立即发送。但是我从来没有对此进行过测试。

  • 如果您的应用程序当前是在打开预定通知时打开的,那么应用程序:didReceiveLocalNotification:被调用,但徽章图标不会更新。因此,为了迎合这种情况,我建议每次启动应用程序时将徽章图标更新到当天(或从背景返回到前台)。

  • 不确定苹果将​​如何使用徽章图标。在审核您的应用时,他们可能会觉得它让用户感到困惑,因为它通常用于指示应用中新增/更改数据的项目数。所以你的应用程序可能会被拒绝。

  • 理想情况下,Apple会为开发人员提供一个API,以便向用户提供这种一目了然的信息,无论是通过更改应用程序实现的以某种方式,或通过小部件等图标/徽章。我上面描述的应该工作,但显然有点克服iOS当前的局限性。内置日历应用程序每天更改其图标以显示该月的日期,我可以看到许多应用程序可以从此行为中受益 - 例如天气应用程序可以显示当前位置的天气状况图标。

  • I'm not sure on how reliable the local notifications are. Push notifications are not guaranteed, but I would hope and expect that local notifications are guaranteed to be delivered. e.g. if the phone is turned off at midnight and only turned on at say 7 AM, I would hope that the local notification scheduled for midnight would be immediately delivered. But I've never tested this.
  • If your app is currently open when a scheduled notification is delivered, the application:didReceiveLocalNotification: is called but the badge icon will not be updated. So in order to cater for this case, I would suggest updating the badge icon to the current day every time the app is launched (or returns to the foreground from the background).
  • Not sure what Apple would make of this use of the badge icon. On reviewing your app, they might feel that it is confusing to the user since it is normally used to indicate the number of items of new/changed data in an app. So your app could get rejected for this.
  • Ideally Apple would provide an API for developers to provide this kind of "at a glance" info to the user, whether that's achieved by changing the application icon/badge in some way, or by a widget etc. What I've described above should work but is clearly a bit of a kludge to get around the current limitations of iOS. The built in calendar app changes its icon every day to show the day of the month and I can see a lot of apps could benefit from this behaviour - e.g. the weather app could show an icon of the weather conditions at the current location.

编辑:

这是一个代码片段,用于取消任何现有的本地通知,并在将来安排64午夜,以及指定为徽章编号的月份的正确日期:

Here's a code snippet to cancel any existing local notifications and schedule 64 in the future for midnight and with the correct day of the month specified as the badge number:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];

[components setHour:0];
[components setMinute:0];
[components setSecond:0];

NSDate* midnightToday = [calendar dateFromComponents:components];

const int SECONDS_PER_DAY = 60 * 60 * 24;

for (int i = 1; i <= 64; i++)
{
    NSDate* futureDate = [midnightToday dateByAddingTimeInterval:i * SECONDS_PER_DAY];
    NSDateComponents* components = [calendar components:NSDayCalendarUnit fromDate:futureDate];

    UILocalNotification* notification = [[UILocalNotification alloc] init];
    notification.fireDate = futureDate;
    notification.timeZone = [NSTimeZone systemTimeZone];
    notification.alertBody = nil;
    notification.soundName = nil;
    notification.applicationIconBadgeNumber = components.day;

    //NSLog(@"futureDate: %@", [NSDateFormatter localizedStringFromDate:futureDate dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]);
    //NSLog(@"notification: %@", notification);        

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    [notification release];
}

[calendar release];

这里发生的事情是:


  • 我们今天的日期是 [NSDate日期] ,然后将其分解为我们要保留的组件(年,月,日),以及然后将时间分量(小时,分钟,秒)设置为午夜。这给了我们 midnightToday

  • 我们循环64次,每次通过 midnightToday 创建一个1到64天的新日期,添加每天的秒数乘以我们希望日期的天数。我们使用此日期来安排本地通知。

  • 我们还需要计算月份的徽章编号。所以我们再次使用 NSDateComponents 将未来日期分解为我们感兴趣的组件 - 在这种情况下只是当天我们指定 NSDayCalendarUnit 。然后我们可以将 applicationIconBadgeNumber 设置为 components.day

  • 每个64个通知是使用UIApplication计划的,将来会在1至64天内发送。

  • We take today's date with [NSDate date], and then break it into the components we want to keep (year, month, day), and then set the time components (hour, minute, second) to be midnight. This gives us midnightToday.
  • We loop through 64 times, each time creating a new date that is from 1 to 64 days in the future by taking midnightToday and adding on the number of seconds per day multiplied by how many days in the future we want the date to be. We use this date to schedule the local notification.
  • We also need to work out the day of the month for the badge number. So again we use NSDateComponents to break the future date into the components we are interested in - which in this case is only the day so we specify NSDayCalendarUnit. We can then set the applicationIconBadgeNumber to be components.day
  • Each of the 64 notifications is scheduled with UIApplication, to be delivered 1 to 64 days in the future.

请注意,这可能仅适用于使用Gregorian(西式)日历的用户 - 根据您的市场情况,您可能需要考虑世界各地使用的其他日历类型并支持这些日历类型。

Note that this will probably only work for users that use the Gregorian (western style) calendar - depending on your market you might need to consider the other calendar types in use around the world and support those too.

这篇关于iOS徽章编号实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆