ios - NSNotificationCenter是否需要取消UILocalnotficiation?

标签 ios objective-c uilocalnotification nsnotificationcenter

我想取消 UILocalnotification,当我取消通知时,通知仍然被触发。我想知道我是否必须调用 appdelegate 中的任何委托(delegate)方法来取消通知。我正在使用的代码正在正确执行。但通知被触发。 我应该使用具有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]);

     }

最佳答案

NSNotification center which has removeObserver method to cancel uilocalnotification.

NSNotificationCenter 与 UILocalNotification 没有任何关系。很抱歉他们的名字中都使用了“通知”,但这确实只是巧合。

关于ios - NSNotificationCenter是否需要取消UILocalnotficiation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16403097/

相关文章:

ios - 从 Parse 中的指针列获取数据(iOS)

iphone - 如何用不同的变量重复一个 Action ?

iOS UIView 没有立即更新

ios - 如何添加FacebookSDK iOS?

ios - MKMapView 中心不正确

ios - 大规模并行计算ios

iphone - 本地通知“每天上午7:00”通知

ios - 安排 iOS 本地推送通知

swift - 尝试实现 beginBackgroundTaskWithExpirationHandler 和 UILocalNotification

c# - UITextField PlaceHolder 颜色更改 Monotouch