【问题标题】:Handle Notifications when app is terminated(iOS)应用程序终止时处理通知(iOS)
【发布时间】:2017-09-01 06:37:05
【问题描述】:

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

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

【问题讨论】:

  • @nyg 我的问题是关于如何在应用程序终止时存储通知。链接是关于在后台状态下处理通知而不是终止状态
  • 根据这个问题的回答stackoverflow.com/questions/35058870/… 似乎没有办法存储通知。但是在whatsapp中它正在工作。

标签: ios apple-push-notifications


【解决方案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
    }
}

【讨论】:

  • 哦,我没试过这个。我会试试这个并检查。感谢您的回复
  • @BharathReddy 太棒了!抱歉,我已将您的问题标记为重复,我没有完全理解您想要做什么......
猜你喜欢
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
相关资源
最近更新 更多