2016-04-01 39 views
2

我安排了一个本地通知,在用户输入特定地理位置6分钟后关闭,前提是他们仍然在此位置。如何更新/删除通知中心的通知?

此定像这样:

UILocalNotification n = new UILocalNotification(); 
n.FireDate = NSDate.FromTimeIntervalSinceNow(360); 
n.AlertAction = "My notification"; 
n.AlertBody = "Notification body";  
UIApplication.SharedApplication.ScheduleLocalNotification(n); 

在一些情况下,某些geolocations重叠,并且用户将接收一个以上的通知。发生这种情况时,我希望能够更新通知中心的通知以反映此情况。

我被告知通知中心的直接更新在iOS9中是不可能的,因此要实现我想要的操作,我需要删除通知并将其替换为新的。

从几个小时的谷歌搜索,我发现了以下方法,所有这些都没有奏效。

UIApplication.SharedApplication.CancelAllLocalNotifications(); 

此功能似乎没有在当前版本的iOS中执行任何操作。

UIApplication.SharedApplication.ApplicationIconBadgeNumber = 1; 
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; 

一些人提到,必须先设置证件号码,然后将其设置为零以从通知中心删除通知。这也没有效果。

foreach(UILocalNotification n in UIApplication.SharedApplication.ScheduledLocalNotifications) 
{ 
    UIApplication.SharedApplication.CancelLocalNotification(n); 
} 

对于一些发现CancelAllNotifications未奏效的尝试上述方法。这似乎也没有效果。如果有帮助,我试图在通知中心显示通知的时候记录一个计数为ScheduledLocalNotifications,但数组回到空白。

List<UILocalNotifications> notifications = new List<UILocalNotifications>(); 

... 

UIApplication.SharedApplication.ScheduleLocalNotification(n); 
notifications.Add(n); 

... 

foreach(UILocalNotification n in notifications) 
{ 
    UIApplication.SharedApplication.CancelLocalNotification(n); 
} 

另一个用户建议保留原始通知,然后使用它们在需要时稍后取消它们。再次,我没有这个运气。

在Objective-C或Swift中的答案也将非常感谢,我可以理解这些语言。

+0

你想删除特定的通知或程序的所有其他通知来自通知中心 – Chetan

+0

@Chetan我想删除来自我的应用的所有其他通知。 – Kizari

+0

您正试图在后台或前台删除通知 – Chetan

回答

1

下面一些方法来保存和删除notificaiton
NoticationName只是任何名义,以识别通知要删除

#pragma mark - Remove Notification 

    - (void) removedStoredLocalNotificationAndCancelNotificationFromPanelOfType: (NSString*) notificationName 
    { 
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

     id storedObject = [defaults objectForKey: notificationName]; 

     if([storedObject isKindOfClass:[NSData class]]) 
     { 
      UILocalNotification* removeNotification = (UILocalNotification*)[NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)[defaults objectForKey:notificationType]]; 

      if(removeNotification) 
      { 
       [[UIApplication sharedApplication]cancelLocalNotification:removeNotification]; 
       [defaults removeObjectForKey: notificationName]; 
      } 
      removeNotification = nil; 
     } 
     else 
     { 
      [defaults removeObjectForKey: notificationName]; 
     } 


     defaults = nil; 

    } 

    #pragma mark -Save notification 
    - (void) saveNewLocalNotificationInUserDefaultsOfType: (NSString*) notificationName withLocalNotification: (UILocalNotification*)localNotification 
    { 

     //************* Check if previous Notification is Present. If YES remove it and show new notification with storing new data in userDefaults**************// 
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     if(![defaults objectForKey:notificationName]) 
     { 
      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotification]; 
      [defaults setObject:data forKey:notificationName]; 
      [defaults synchronize]; 
     } 
     else if ([defaults objectForKey:notificationName]) 
     { 

      // Remove previous notification 
      //Check if any observation notification is present.If YES remove it from user defaults if stored and cancel local notification 

      [self removedStoredLocalNotificationAndCancelNotificationFromPanelOfType:notificationName]; 

      //Store new notification 

      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotification]; 
      [defaults setObject:data forKey:notificationName]; 
      [defaults synchronize]; 
     } 

    } 
+0

谢谢你。不幸的是,这也没有从通知中心删除通知。看起来好像这些通知并没有首先将它放入'UIApplication.SharedApplication.ScheduledLocalNotifications'数组中,这可能就是为什么这些代码片段都不能将它们移除的原因。我不明白什么会导致这种情况发生,因为我通过'UIApplication.SharedApplication调度通知。ScheduleLocalNotification“,这是iOS中的标准程序。 – Kizari

+0

如果可能的话,你可以分享代码吗?如果,我可以研究它。 – Chetan