我想取消UILocalnotification,当我取消通知仍然通知被解雇。我想知道我是否必须调用appdelegate中的任何委托方法来取消通知。我使用的代码正在正确执行,但通知被解雇了。 我应该使用NSNotification中心有removeObserver方法取消uilocalnotification。NSNotificationCenter是否需要取消UILocalnotficiation?
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]);
}
是否强制将通知作为参数传递,或者它可以成为补救ID ..因为我传递remedyID原因,如果remedyID与notificationID相同,那么我取消通知..但我仍然收到通知 – raptor 2013-05-06 17:13:22
是的,您需要通过通知。检查我更新的答案。 – 2013-05-06 17:18:48
我编辑了我的答案。检查出来.. – 2013-05-06 17:23:05