簡體   English   中英

應用程序終止時處理通知(iOS)

[英]Handle Notifications when app is terminated(iOS)

我正在開發接收通知的應用程序(使用蘋果推送通知)。 我正在存儲這些通知並在一個控制器中顯示為列表。 據我所知,只要收到通知,就會調用didReceiveRemoteNotification。 當應用程序處於前台和后台時,我能夠通過didReceiveRemoteNotification方法在Db中存儲通​​知。 但是,當應用程序終止時,我如何存儲通知?

如果用戶在應用程序終止時點擊通知,我可以使用lauch選項存儲通知。 但是,如果用戶在應用程序終止時未點擊通知,我該如何存儲通知?

您是否嘗試過使用getDeliveredNotifications(completionHandler:) 您可以在控制器的viewDidLoad方法中調用以下代碼,您可以在其中顯示收到的通知。

請注意,如果用戶清除通知中心收到的通知,則無效。 您必須按照您在評論中鏈接的問題的接受答案中提到的那樣做。

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    // notifications: An array of UNNotification objects representing the local
    // and remote notifications of your app that have been delivered and are still
    // visible in Notification Center. If none of your app’s notifications are
    // visible in Notification Center, the array is empty.

    // As said in the documentation, this closure may be executed in a background
    // thread, so if you want to update your UI you'll need to do the following:
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}

暫無
暫無

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

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