【发布时间】:2020-04-19 05:45:04
【问题描述】:
我正在使用didReceiveRemoteNotification 来检测应用通知。但是当应用程序处于运行状态时,它会自动触发。我需要在应用程序处于运行状态时检测到通知选择,而不是通过didReceiveRemoteNotification 自动检测通知。提前致谢
【问题讨论】:
标签: ios swift push-notification apple-push-notifications push
我正在使用didReceiveRemoteNotification 来检测应用通知。但是当应用程序处于运行状态时,它会自动触发。我需要在应用程序处于运行状态时检测到通知选择,而不是通过didReceiveRemoteNotification 自动检测通知。提前致谢
【问题讨论】:
标签: ios swift push-notification apple-push-notifications push
当应用在前台运行时,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()
}
}
详情:
【讨论】: