每隔30秒UILocalNotification [英] UILocalNotification every 30 seconds
问题描述
我正在尝试使用以下逻辑设置 UILocalNotification
每30秒运行一次,但它似乎行为不端。有两个问题:
I am trying to setup a UILocalNotification
to run every 30 seconds using the following logic, however it seems to be misbehaving. There are 2 issues:
- 当通知被解雇时,似乎有很多都是一次性的,而不是每30秒一次。
- 应用程序图标徽章编号似乎没有增加。它只停留在1。
请有人帮我弄清楚我做错了吗?
Please can someone help me work out what I've done wrong?
// Create 'base' notification we can use
UILocalNotification *baseNotification = [[UILocalNotification alloc] init];
baseNotification.timeZone = [NSTimeZone defaultTimeZone];
baseNotification.repeatInterval = NSMinuteCalendarUnit;
baseNotification.alertBody = @"My Message.";
baseNotification.alertAction = @"My Alert Action";
baseNotification.soundName = UILocalNotificationDefaultSoundName;
UILocalNotification *alertOne = [baseNotification copy];
alertOne.applicationIconBadgeNumber++;
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:alertOne];
UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.applicationIconBadgeNumber++;
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
[[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];
推荐答案
目前无法实现自定义重复间隔。
There is currently no way possible to achieve custom repeats with intervals.
但是,通知系统最多可以排队64个通知,所以你最接近的事情就是根据需要手动设置多个通知(每个通知都有不同的通知)徽章的编号和另一个fireDate)然后通过设置新的通知列表来更新你的通知列表。
However, the notification system can queue up to 64 notifications so the closest thing you could do is to manually set as many notifications as you need (with each one having a different number for the badge and a different fireDate) and then have your notifications list updated by setting new ones when you're running low on them.
这将返回通知数量你在队列中:
This will return how many notifications you've in queue:
[[[UIApplication sharedApplication] scheduledLocalNotifications] count]
我还建议你阅读这篇文章以获得进一步的帮助:
There's also this post that I would recommend you reading for further help:
祝你好运!
这篇关于每隔30秒UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!