簡體   English   中英

當應用程序終止時處理點擊本地通知 - iOS - Swift

[英]Handle tap on local notification when app is terminated - iOS - Swift

當應用程序終止時,我想處理本地通知的點擊,但是方法application didFinishLaunchingWithOptions中沒有啟動選項,也沒有調用方法application didReceive notification

我還嘗試實現UNUserNotificationCenterDelegate的兩種方法:

1)處理通知,當應用程序處於后台但未終止時

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
)

2)接下來處理通知,當應用程序在前台時:

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
)

不知道如何處理它...

當您點擊通知中心的推送通知時。 操作系統啟動您的應用程序(之前已終止),然后仍然將操作傳遞給此(功能 1)

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
)

您可以像您所做的那樣從響應中獲得notification object。 但是您可能需要從選定的推送通知中臨時保存此數據,然后在應用程序完全與用戶激活時處理它。

請記住在完成邏輯時調用completionHandler() 並且,您必須設置UNUserNotificationCenter.current().delegate = self ,然后再return true

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool`.

self這里是您的AppDelegate ,這意味着您將 UserNotification 的委托設置為實時 object 或初始化 object。它將能夠處理上述委托 function 調用(功能 1)以獲取您選擇的推送通知的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM