首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用UserNotifications框架从通知中心删除远程通知

如何使用UserNotifications框架从通知中心删除远程通知
EN

Stack Overflow用户
提问于 2017-02-28 20:38:14
回答 2查看 3.9K关注 0票数 2

我在这里和其他地方看到过几个线程,但是似乎没有一个使用用于iOS 10的新的iOS框架。

有一个实例方法getDeliveredNotifications(completionHandler:)UNUserNotificationCenter单例函数current()上调用。

completionHandler:接受一个传递通知数组,然后可以使用removeDeliveredNotifications(withIdentifiers:)在块内删除这些通知。

代码语言:javascript
复制
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in 
    // UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [String])
}

我的挑战是如何从所有交付的通知中识别特定的通知,然后删除它?

这就是我现在要做的,看看是否有一个远程通知,其中包含我从服务器发送的id,其中包含了有效载荷键ID。这并不会删除有问题的通知,显然是因为第一个函数返回零,尽管通知在通知中心是可见的。

代码语言:javascript
复制
func isThereANotificationForID(_ ID: Int) -> UNNotification? {    
    var foundNotification: UNNotification?

    UNUserNotificationCenter.current().getDeliveredNotifications {
        DispatchQueue.main.async {
            for notification in notifications {
                if notification.request.content.userInfo["id"] as! Int == ID {
                    foundNotification = notification
                }
            }
        }
    }

    return foundNotification
}

func removeNotification(_ notification: UNNotification) {
    UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notification.request.identifier])
}

// Find the notification and remove it
if let deliveredNotification = isThereANotificationForID(ID) {
    removeNotification(deliveredNotification)
}
EN

回答 2

Stack Overflow用户

发布于 2019-10-05 11:45:55

请注意,这个答案是使用Swift 5并在iOS 13上测试的。

我们可以扩展UNUserNotificationCenter以删除我们选择的通知。就我个人而言,我按线程对它们进行分组,并以这种方式删除它们,但是这个扩展还包括一种按字典对删除它们的方法。

代码语言:javascript
复制
extension UNUserNotificationCenter {
    func decreaseBadgeCount(by notificationsRemoved: Int? = nil) {
        let notificationsRemoved = notificationsRemoved ?? 1
        DispatchQueue.main.async {
            UIApplication.shared.applicationIconBadgeNumber -= notificationsRemoved
        }
    }

    func removeNotifications(_ notifications: [UNNotification], decreaseBadgeCount: Bool = false) {
        let identifiers = notifications.map { $0.request.identifier }
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
        if decreaseBadgeCount {
            self.decreaseBadgeCount(by: notifications.count)
        }
    }

    func removeNotifications<T: Comparable>(whereKey key: AnyHashable, hasValue value: T, decreaseBadgeCount: Bool = false) {
        UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
            let notificationsToRemove = notifications.filter {
                guard let userInfoValue = $0.request.content.userInfo[key] as? T else { return false }
                return userInfoValue == value
            }
            self.removeNotifications(notificationsToRemove, decreaseBadgeCount: decreaseBadgeCount)
        }
    }

    func removeNotifications(withThreadIdentifier threadIdentifier: String, decreaseBadgeCount: Bool = false) {
        UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
            let notificationsToRemove = notifications.filter { $0.request.content.threadIdentifier == threadIdentifier }
            self.removeNotifications(notificationsToRemove, decreaseBadgeCount: decreaseBadgeCount)
        }
    }

    func removeNotification(_ notification: UNNotification, decreaseBadgeCount: Bool = false) {
        removeNotifications([notification], decreaseBadgeCount: decreaseBadgeCount)
    }
}

有了这个分机,您可以调用,例如,

代码语言:javascript
复制
UNUserNotificationCenter.current().removeNotifications(whereKey: "id", hasValue: 123)

如果您还想减少应用程序图标上的徽章编号,可以设置decreaseBadgeCount: true,或者交替调用UNUserNotificationCenter.current().decreaseBadgeCount

票数 2
EN

Stack Overflow用户

发布于 2018-09-26 11:59:14

以前的removeDeliveredNotifications不能正常工作。似乎苹果是固定的问题,因为它将通过使用单行代码为我工作。

代码语言:javascript
复制
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: aryIdentifier)

我在模拟器里试了5-6个小时来清除本地通知,但是没有运气。当我将运行代码在实际的设备,它会喜欢的魅力。

注:请在真实设备上进行测试,以上方法不适用于模拟器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42518286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档