0

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.

Timmy
  • 4,098
  • 2
  • 14
  • 34
Vileriu
  • 25
  • 4

1 Answers1

0

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.

neskafesha
  • 191
  • 5
  • I added already but i need to reach UNNotification instance in didReceiveRemoteNotification functions as with the other functions i mentioned. – Vileriu Oct 27 '22 at 18:01
  • Hmm, you won't get UNNotification object in this function. But you can manual generate an UNNotification object with data in userInfo in didReceiveRemoteNotification. – neskafesha Oct 27 '22 at 18:44
  • Could you please help me how can i? – Vileriu Oct 28 '22 at 08:44
  • https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification see this domumentation. Add in notification sending json your identifier and date. – neskafesha Oct 28 '22 at 10:17
  • You did not understand what i told. – Vileriu Oct 28 '22 at 14:07