2

我开发了一个严重依赖本地通知的应用程序。自从升级到 ios 11.2.1 后,行为发生了变化,而我的代码没有任何变化。

在 11.2.0 之前的上一个版本中,如果通知中心有来自同一应用程序的多个通知,并且其中一个得到了答复,则所有其他通知都会保留在通知中心(除非被应用程序内的代码清除)。

升级到 11.2.1 后,每次收到通知时,所有其他通知也会被删除。我在模拟器和我的 iPhone 6 上测试了多台设备。这也发生在其他应用程序上,如 Messages 或 Line,因此,它不仅限于本地通知。

这是 ios 11.2.1 中的错误还是预期的通知新行为?无论哪种情况,有没有办法让通知像以前一样工作?

这是一个如何相关的代码处理答案

- (void)removeNotificationWithIdentifier:(NSString *)identifier {
    NSMutableDictionary *notifications = [self getAllNotifications];
    NSMutableDictionary *categories = [self getAllCategories];
    UNNotificationContent *content = [notifications objectForKey:identifier];
    NSString *category = [content.userInfo objectForKey:@"category"];

    [notifications removeObjectForKey:identifier];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center removePendingNotificationRequestsWithIdentifiers:[NSArray arrayWithObjects:identifier, nil]];
    [center removeDeliveredNotificationsWithIdentifiers:[NSArray arrayWithObjects:identifier, nil]];

    if (category != nil) {
        [categories removeObjectForKey:category];
    }

    [self saveAllNotifications:notifications];
    [self saveAllCategories:categories];
}
4

0 回答 0