swift 如何在didReceiveRemoteNotification中获取通知响应值?

5t7ly7z5  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(376)

我在didReceivewillPresentgetDeliverredNotifications函数上达到了UINotification值。但我无法达到didReceiveRemoteNotification中的通知值。我想要达到通知,因为通知值具有标识符和日期信息。我将通知数据存储到设备,我需要该信息。

extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){

    let userInfo = response.notification.request.content.userInfo
    let id = response.notification.request.identifier
    let date = response.notification.date

    completionHandler()
  }

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    guard let aps = userInfo["aps"] as? [String: AnyObject] else {
      completionHandler(.failed)

      return
    }
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
    let userInfo = notification.request.content.userInfo
    let id = notification.request.identifier
    let date = notification.date

    return [[.banner, .badge, .list, .sound]]
  }
}

我尝试在didReceiveRemoteNotification中获取通知数据,但无法获取。

tvz2xvvm

tvz2xvvm1#

在服务器端创建推送通知时,将此数据放入didReceiveRemoteNotification的userInfo中获取唯一id和日期,可以在服务器端获取当前日期或在didReceiveRemoteNotification中调用Date()。

相关问题