UILocalNotification every 30 seconds

天涯浪子 提交于 2019-12-01 12:19:38
jbrodriguez

There is currently no way possible to achieve custom repeats with intervals.

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:

iOS badge number live update

Good luck!

Try this one.

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.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
alertOne.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
alertTwo.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

Regarding second point, you're increasing the badge number of the copy not the original notification. And since the original has a zero badge number you'll always get a copy with zero badge number too and increasing it will make it always 1.

The solution is to increase the badge of the original notification right before making the copy:

...
baseNotification.applicationIconBadgeNumber++;
UILocalNotification *alertOne = [baseNotification copy];
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:alertOne];

baseNotification.applicationIconBadgeNumber++;
UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
[[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];

According NSObject class reference :

copy - Returns the object returned by copyWithZone:

And copyWithZone returns a shallow copy. So its like all notification have same properties . Hence , badge number is always "1" and fireDate is same for all notifications. i.e. last one that you apply . Hence , notifications get fired at same time.

Hope , it helps.

I think you get a lot of notifications every 30 seconds is because you did not cancel previous notifications. Add this line at the top of your code.

[[UIApplication sharedApplication] cancelAllLocalNotifications];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!