ios - Swift:从远程通知获取数据

标签 ios swift remote-notifications silent-notification

我有一个消息传递应用程序,它通过推送通知从服务器获取数据。 在下图中,您可以看到进程并检查它在哪里出现故障:enter image description here

我对这个问题进行了很多研究,但没有找到一个通用的解决方案。

我得到的一个解决方案是使用静默通知,discussed Here , 在显示数据之前获取数据并保存。但是我没有找到如何在收到静默通知后显示来自 app delegate 的通知。

如果我在某个地方概念上有误,请纠正我。

更新: 问题之一是 application:didReceiveRemoteNotification:fetchCompletionHandler: 仅在

时调用
  1. 应用在前台
  2. 用户点击通知

但是当用户没有点击通知时,application:didReceiveRemoteNotification:fetchCompletionHandler: 没有被调用,我找不到保存数据的方法,出现在通知中。

最佳答案

你的问题是“在应用程序处于前台时从远程通知获取数据”如果我是正确的,你可以使用这两种方法从后台和前台应用程序模式的远程通知获取数据。

//  Notification will present call back
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound, .badge])

    // Handle your data here
    print("Notification data: \(notification.request.content.userInfo)")
}

@available(iOS 10.0, *)
//  Notification interaction response call back
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    // Handle your data here
    print("Notification data: \(response.notification.request.content.userInfo)")

}

关于ios - Swift:从远程通知获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514256/

相关文章:

objective-c - iOS5下添加SplitViewController View 麻烦

ios - 本地化应用程序因 NSRangeException 而崩溃

arrays - 如何在 Swift 中打乱数组?

收到推送通知时 iOS 13 崩溃?

ios - UICollectionView 装饰 View

ios - 有没有办法在不使用 UINavigationController 的情况下更改 Storyboard 中 UINavigationBar 的高度?

ios - 如何动态更改本地化 SwiftUI

swift - 如何在 View 中获取子 Sprite 位置

ios决定是否在客户端代码的某些条件下静音我的远程通知,而不是设置内容可用

ios - iOS中的远程通知和静默通知有什么区别?