当应用在 iOS 中处于运行状态时检测推送通知选择



我正在使用didReceiveRemoteNotification来检测应用程序通知。但是当应用程序处于运行状态时,它会自动触发。我需要在应用程序处于运行状态时检测到通知选择,而不是通过didReceiveRemoteNotification自动检测通知。提前致谢

>iOS 10+ 提供自定义本地通知,以便在应用在前台运行时处理此类问题。

didFinishLaunchingWithOptions中,添加委托

UNUserNotificationCenter.current().delegate = self

然后创建一个应用委托扩展并添加此扩展。

@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()
}
}

详情:

阅读本教程

最新更新