是否需要 NSNotificationCenter 来取消 UILocalnotficiation?
Posted
技术标签:
【中文标题】是否需要 NSNotificationCenter 来取消 UILocalnotficiation?【英文标题】:Is NSNotificationCenter necessary to cancel UILocalnotficiation? 【发布时间】:2013-05-06 16:32:28 【问题描述】:我想取消 UILocalnotification ,当我取消通知时,通知仍然被触发。我想知道我是否必须调用 appdelegate 中的任何委托方法来取消通知。我正在使用的代码正在正确执行。但是通知被触发了。 我应该使用具有 removeObserver 方法的 NSNotification 中心来取消 uilocalnotification。
UILocalnotification 是从应用还是从设备触发通知。
更新 - 这就是我安排通知的方式
-(UILocalNotification *)scheduleNotification :(int)remedyID
NSString *descriptionBody;
NSInteger frequency;
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSLog(@"%d",remedyID);
descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"];
frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue];
NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
for (NSDate *fireDate in notificationFireDates)
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.fireDate = fireDate;
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody, @"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
nil];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
return notif;
取消通知
- (void)cancelNotification:(int)remedyId
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
for (UILocalNotification *notification in notifications)
int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue];
NSLog(@"remedyID : %d",remedyId);
NSLog(@"notifyId : %d",notifRemedyId);
if (remedyId == notifRemedyId)
[[UIApplication sharedApplication] cancelLocalNotification:notification];
NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
【问题讨论】:
【参考方案1】:NSNotification 中心有 removeObserver 方法来取消 uilocalnotification。
NSNotificationCenter 与 UILocalNotification 没有任何关系。很抱歉他们都在名字中的某个地方使用了“通知”,但这只是巧合。
【讨论】:
【参考方案2】:scheduledLocalNotifications 会给你所有预定通知的列表和使用
- (void)cancelLocalNotification:(UILocalNotification *)notification
或者您可以使用以下命令全部取消:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
编辑:
NSString *notifRemedyId = @"notifRemedyId";
UILocalNotification *localnotif=[[UILocalNotification alloc]init];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:@"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,@"notifRemedyId",nil];
localnotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif];
取消为:
for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications])
if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:@"kRemindMeNotificationRemedyIDKey"])
if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue])
[[UIApplication sharedApplication] cancelLocalNotification:aNotif];
break;
希望对你有帮助。
【讨论】:
是否必须将通知作为参数传递,或者它可以是remediateID ..因为我正在传递remediateID 因为如果remediateID 与notificationID 相同,那么我正在取消通知..但我仍然收到通知 我正在比较remediateID 和notifRemedyID .. EDIT 中的第一个代码我应该在schedulenotification 中添加它并在我的取消通知方法中使用第二个代码吗?我已经添加了我的 scheduleNotification 方法以上是关于是否需要 NSNotificationCenter 来取消 UILocalnotficiation?的主要内容,如果未能解决你的问题,请参考以下文章
是否可以替代使用 NSNotificationCenter 和 kFIRInstanceIDTokenRefreshNotification 来检测过期令牌?
WiFi网络更改是不是有NSNotificationCenter通知?
Objective-C - 将 NSNotificationCenter 放在哪里?