[英]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()
}
}
詳情:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.