Handle Notifications when app is terminated(iOS)

烈酒焚心 提交于 2020-01-04 02:27:35

问题


I am working on app which receives notifications(using apple push notifications). I am storing these notifications and showing as list in one controller. As far as I understand whenever notification is received didReceiveRemoteNotification is called. When app is in foreground and background I am able to store notification in Db from didReceiveRemoteNotification method. But when app is terminated how can I store notifications?

If user taps on notification when application is terminated I am able to store notification by using lauch options. But if user does not tap the notification when app is terminated how do I store the notification?


回答1:


Have you tried using getDeliveredNotifications(completionHandler:)? You can call the following code in e.g. the viewDidLoad method of the controller in which you display the received notifications.

Note that if the user clears the received notifications in the notification center this won't work. You'll have to do as mentioned in the accepted answer of the question you linked in your comment.

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
    }
}


来源:https://stackoverflow.com/questions/45994511/handle-notifications-when-app-is-terminatedios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!