0

how to resolve the issue stated above. Thank You.

let notificationTypes : UIUserNotificationType = [UIUserNotificationType.alert , UIUserNotificationType.badge, UIUserNotificationType.sound]
let notificationSettings = UIUserNotificationSettings(types: notificationTypes,categories : nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)

return true

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

print("MessageID: \(userInfo["gcm_message_id"])") //the error is in this line
print(userInfo)

}

1 Answers1

1

The error states that you're trying to access a nil value. You should conditionally unwrap the value so that it doesn't try to access to the nil value.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

  if let message = userInfo["gcm_message_id"] {
    print("MessageID: \(message)")
  }
  print(userInfo)
}
patngo
  • 667
  • 3
  • 12