应用终止时处理通知(iOS) [英] Handle Notifications when app is terminated(iOS)

查看:86
本文介绍了应用终止时处理通知(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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?

推荐答案

您是否尝试过使用

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

这篇关于应用终止时处理通知(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆