elk*_*orb 8 apple-push-notifications ios swift
如果从用户通知到达通知中心的时间过去了超过一分钟,则有一个“清除”选项可以一次从通知中心取消一个或多个通知。
iOS 操作系统如何通知用户点击“清除”以同时关闭多个通知?
Van 的 anwser 直奔正确的方向,但我们不需要实施自定义操作来获得提问者想要的东西。
如果您创建类别并将其传递给 UNUserNotificationCenter,即使用户选择了内置清除按钮或内容扩展上的“X”按钮,您也会收到委托 didReceive 函数的回调。然后 ResponeIdentifier 将是response.actionIdentifier == UNNotificationDismissActionIdentifier。
类别必须是这样的:
//Create the category...
UNNotificationCategory(identifier: "YourCustomIdentifier",
actions: [], intentIdentifiers: [], options: .customDismissAction)
//... and pass it to the UNUserNotificationCenter
UNUserNotificationCenter.current().setNotificationCategories(notificationCategories)
Run Code Online (Sandbox Code Playgroud)
类别触发了 iOS 框架中的魔法,突然间你在你的委托中得到了回调。委托函数应如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == UNNotificationDismissActionIdentifier {
// notification has been dismissed somehow
}
completionHandler()
}
Run Code Online (Sandbox Code Playgroud)
从 iOS 10 及更高版本开始可以实现自定义通知,您需要使用 UNNotificaitons
private func registerForRemoteNotificaiton(_ application: UIApplication) {
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {(granted, error) in
if granted {
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
})
configureUserNotification()
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self as MessagingDelegate
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
// [END register_for_notifications]
}
private func configureUserNotification() {
if #available(iOS 10.0, *) {
let action = UNNotificationAction(identifier: UNNotificationDismissActionIdentifier, title: "Cancel", options: [])
//let action1 = UNNotificationAction(identifier: "dismiss", title: "OK", options: [])
let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [action], intentIdentifiers: [], options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([category])
} else {
// Fallback on earlier versions
}
}
Run Code Online (Sandbox Code Playgroud)
从 appdelegate 的 didFinishLaunching 方法调用 registerForRemoteNotificaiton 方法。
然后你需要在appdelegate中实现UNUserNotificationCenterDelegate。
然后你就会明白(这里是我们在操作名称中添加的“关闭”)
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == UNNotificationDismissActionIdentifier {
//user dismissed the notifcaiton:
}
completionHandler()
}
Run Code Online (Sandbox Code Playgroud)
在这里找到更多信息