【问题标题】:How to overwrite or update a UILocalNotification如何覆盖或更新 UILocalNotification
【发布时间】:2014-05-29 07:16:00
【问题描述】:

我正在使用UILocalNotification 对象向我的应用程序发出通知。目前每次生成事件时,我都会弹出一个通知。

  notification.alertBody=@"Event Occurs 2";
  notification.alertAction=@"Open";
  [[UIApplication sharedApplication]presentLocalNotificationNow:notification];

但是,由于事件不断发生,每次都会生成新的通知。

如果通知已经存在,有没有办法更新通知,如果不存在,则创建新通知。

【问题讨论】:

    标签: ios objective-c uilocalnotification


    【解决方案1】:

    您无法更新已安排的本地通知。但是,您可以取消它并重新安排一个新的。

    取消您的本地通知:

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *eventArray = [app scheduledLocalNotifications];
    for (int i=0; i<[eventArray count]; i++)
    {
        UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
        NSDictionary *userInfoCurrent = oneEvent.userInfo;
        NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
        if ([uid isEqualToString:uidtodelete])
        {
            //Cancelling the specific local notification
            [app cancelLocalNotification:oneEvent];
            //Schedule your new "updated" local notification here.
            break;
        }
    }
    

    这将遍历所有计划的本地通知并删除您要删除的本地通知。请注意,您需要为每个通知设置一个唯一属性以区分其他通知(在上面的示例中,假设 userInfo 包含一个唯一的“uid”)。

    感谢 KingofBliss 在how to delete specific local notifications 上提供上述代码。

    【讨论】:

    • 这是否记录在某处?因为推送通知可以更新。
    • 我找不到任何有关更新已安排通知的文档。请注意 UILocalNotification 已被弃用,您现在应该使用 UNNotificationRequest 代替。最好的起点是developer.apple.com/documentation/usernotifications/…
    • 我刚刚尝试了 UNNotificationRequest 并发送了具有相同标识符的通知。它得到了更新。虽然我看到多个警报,但通知气泡是相同的。
    【解决方案2】:

    无法更新通知,但如果您将带有键的字典附加到警报:

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    [userInfo setObject:alarmID forKey:@"AlarmKey"];
    // Set some extra info to your alarm
    notification.userInfo = userInfo;
    

    然后您可以检索本地通知,取消它并使用更新的内容创建一个新通知。

    + (UILocalNotification *)existingNotificationWithAlarmID:(NSString *)alarmID
    {
        for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
            if ([[notification.userInfo objectForKey:@"AlarmKey"] isEqualToString:alarmID]) {
                return notification;
            }
        }
    
        return nil;
    }
    

    您可以这样取消通知:

    - (void)cleanUpLocalNotificationWithAlarmID:(NSString *)alarmID
    {
        UILocalNotification *notification = [self existingNotificationWithAlarmID:alarmID];
        if (notification) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }
    

    【讨论】:

      【解决方案3】:

      不,无法修改已安排的本地通知。您必须取消通知并重新安排。

      【讨论】:

        猜你喜欢
        • 2014-10-14
        • 2010-09-24
        • 1970-01-01
        • 2023-03-19
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2017-08-11
        • 1970-01-01
        相关资源
        最近更新 更多