swiftpush-notificationnotificationsapple-push-notificationsunnotificationrequest

How can i get notification response value in didReceiveRemoteNotification?


I reached UINotification value on didReceive, willPresent and getDeliverredNotifications functions. But i can't reach notification value in didReceiveRemoteNotification. I want to reach notification, because notification value has got identifier and date informations. I store notification data to device and i need that information.

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

I tried to get notification data in didReceiveRemoteNotification but i couldn't.


Solution

  • When creating push notifications on the server side, put this data to get unique id and date in userInfo in didReceiveRemoteNotification. You can get the current date on the server or call Date() in didReceiveRemoteNotification.