当我记录本地通知的描述时
`<UIConcreteLocalNotification: 0x1edd4d40>{fire date = Thursday, September 26, 2013, 11:15:00 AM India Standard Time, time zone = Asia/Kolkata (GMT+05:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, September 26, 2013, 11:15:00 AM India Standard Time, user info = {
UID = "38BF41F8-05D3-48E2-A20F-7B84609F4E85";
}}`
我找到了“重复计数”参数。是否有任何选项可以设置重复计数,以便重复此计数并过期
此问题提出了一个可能的解决方法。 取消单次重复本地通知
但是,正如这表明的那样,我认为不可能通过重复计数来区分重复的本地通知。
您无法为此目的设置任何重复计数。但是您可以在收到通知后进行必要的检查来修改 fireDate,然后可以再次安排。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
localNotif = notification;
int no_of_days = [self getNumberOfDaysToNextAlarmInDays];
int day = 24*60*60;
NSTimeInterval interval = day *no_of_days;
localNotif.fireDate = [NSDate dateWithTimeInterval:interval sinceDate:[NSDate date]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
-(int)getNumberOfDaysToNextAlarmInDays {
//do the necessary calculations here
return count ;
}
这里的一个小问题是它仅在调用 didReceiveLocalNotification 时才有效。
您可以将重复计数作为数字给出。当在
Appdelegate
中触发通知时,将调用 didReceiveLocalNotification
方法。在该方法中,您可以获得重复计数。您可以在此处减少重复次数并重新安排通知时间。如果重复次数为 0 那么您无需重新安排 notification
。
例如:
if ([[notification.userInfo objectForKey:@"repeat"]intValue] != 0) {
//You can again schedule your notification here with decrementing the repeat count.
}else{
}
//Here you can cancel the current notification
[[UIApplication sharedApplication]cancelLocalNotification:notification];
希望这有帮助:)
这是一篇旧帖子,但我只是有一个类似的事情,也许它对某些人有帮助:我在上面提出了类似的建议,但我只是在
UNMutableNotificationContent
中添加了一个到期日期
content.userInfo = ["expiration": expirationDate]
然后在通知中心委托中我可以检查这一点并删除过期的通知。像这样的东西:
在UNUserNotificationCenterDelegate中,将呈现通知:
let expDate = notification.request.content.userInfo["expiration"] as? Date