在 iOS 中应用处于 运行 状态时检测推送通知选择
Detect push notification selection when the app is at running state in iOS
我正在使用 didReceiveRemoteNotification
检测应用程序通知。但是当应用程序处于 运行 状态时,它会自动触发。我需要在应用程序处于 运行 状态时检测到 通知选择 ,而不是通过 didReceiveRemoteNotification
自动检测通知。提前致谢
iOS 10+ 提供自定义本地通知,用于在应用 运行 处于前台时处理此类问题。
在didFinishLaunchingWithOptions
中添加委托
UNUserNotificationCenter.current().delegate = self
然后创建一个 appDelegate 扩展并添加它。
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print("tap on on forground app",userInfo)
completionHandler()
}
}
详情:
我正在使用 didReceiveRemoteNotification
检测应用程序通知。但是当应用程序处于 运行 状态时,它会自动触发。我需要在应用程序处于 运行 状态时检测到 通知选择 ,而不是通过 didReceiveRemoteNotification
自动检测通知。提前致谢
iOS 10+ 提供自定义本地通知,用于在应用 运行 处于前台时处理此类问题。
在didFinishLaunchingWithOptions
中添加委托
UNUserNotificationCenter.current().delegate = self
然后创建一个 appDelegate 扩展并添加它。
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print("tap on on forground app",userInfo)
completionHandler()
}
}
详情: