簡體   English   中英

終止應用程序時如何確定是從NotificationCenter(本地通知)打開應用程序還是從應用程序圖標打開

[英]How to determine whether the app is opened from NotificationCenter (Local Notification) or the app icon when app is killed

我的應用程序收到本地通知,而不是遠程通知。 我如何知道應用程序被殺死(不是在后台或前台運行)時是否通過按通知而不是通過應用程序圖標打開了應用程序。 以前,我使用以下方法

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if ((launchOptions?[UIApplication.LaunchOptionsKey.loaclNotification] != nil))
    {
        print("here")
    }
 }

但是從iOS 10開始不推薦使用此功能

[UIApplication.LaunchOptionsKey.loaclNotification] != nil)

蘋果文檔建議使用以下方法。

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

}

僅當應用程序處於前台或后台時才調用此方法。 任何幫助將不勝感激

我怎么知道是否通過按通知而不是按應用程序圖標打開了應用程序

正如你所說的,實現UNUserNotificationCenterDelegate方法didReceive 如果用戶點擊您的通知,它將被調用,如果您的應用未運行,則會啟動您的應用。

暫無
暫無

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

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