2017-09-01 83 views
1

我正在接收通知(使用苹果推送通知)的应用程序。我将这些通知存储在一个控制器中并显示为列表。据我了解,每当收到通知didReceiveRemoteNotification被调用。当应用程序处于前台和后台时,我可以通过didReceiveRemoteNotification方法将通知存储在Db中。但是,当应用程序被终止时,我如何存储通知?应用程序终止时的句柄通知(iOS)

如果用户在应用程序终止时点击通知,我可以使用lauch选项存储通知。但是,如果用户在应用程序终止时未点击通知,我该如何存储通知?

+0

@nyg我的问题是关于如何在应用程序终止时存储通知。链接是关于处理通知在后台状态没有终止状态 –

+0

基于这个问题的答案https://stackoverflow.com/questions/35058870/handling-push-notifications-when-app-is-ter-minated似乎没有办法存储通知。但在whatsapp它正在工作。 –

回答

1

您是否尝试过使用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 
    } 
} 
+0

哦,我没有试过这个。我会尝试这个并检查。谢谢你的回复 –

+0

嘿,它工作。谢谢兄弟 –

+0

@BharathReddy太棒了!对不起,我把你的问题标记为重复,我还没有完全明白你想要做什么...... – nyg