删除特定notification

我的app已经开启了remote notification, 并且当app处于后台状态的时候调用[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[request.identifier]]是可以正常的删除对应的推送通知.但是当我把APP杀死之后,调用该方法也无法删除对应的推送,只有当进入app的时候才会删除对应的推送.请问有什么解决办法吗?

    NSString *key = userInfo[@"aps"][@"extendInfo"][@"name"];
    NSString *type = userInfo[@"aps"][@"alert"][@"title"];
    
    if ([type isEqualToString:@"deleteChannel"]) {
        [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
            if (notifications.count == 0) {
                completionHandler(UIBackgroundFetchResultNewData);
                return;
            }
            else {
                for (UNNotification *notice in notifications) {
                    UNNotificationRequest *request = notice.request;
                    UNNotificationContent *content = request.content;
                    NSDictionary *userInfo1 = content.userInfo;
                    NSString *title = userInfo1[@"aps"][@"alert"][@"body"];
                    if ([title isEqualToString: key]) {
                        [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[request.identifier]];
                        break;
                    }
                    
                }
                completionHandler(UIBackgroundFetchResultNewData);
            }
        }];
    }
    else {
        switch (application.applicationState) {
            case UIApplicationStateActive:
            case UIApplicationStateInactive:
            case UIApplicationStateBackground: {
                completionHandler(UIBackgroundFetchResultNewData);
                break;
            }
            default:
                break;

        }
    }
阅读 2.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题