如何更新/删除通知中心的通知?

时间:2016-04-01 07:02:31

标签: c# ios cocoa-touch xamarin uilocalnotification

我安排本地通知在用户输入特定地理位置后的6分钟内完成,前提是他们仍在此位置。

这样安排如下:

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

在某些情况下,某些地理位置重叠,用户将收到多个通知。发生这种情况时,我希望能够更新通知中心的通知以反映这一点。

我被告知在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中的答案也非常感谢,我可以理解这些语言中的任何一种。

1 个答案:

答案 0 :(得分:1)

以下是保存和删除通知的方法 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];
        }

    }