My iPhone App crashes after getting push notification in some devices(especially ios_10.3.1)

Viewed 2476

I have just figured out how to send push notification correctly in my app. However when it started working correctly, a new type of error has arrived. My app crashes on launch after receiving push notification. I tested on 5 devices and 2 of them crashed due to the issue(both running on iOS_10.3.1). The weird part is the other 3 devices were running on iOS 10.2 and 9.3.1. I don't really think this is something related to OS.

Apple have send a crash log like this but when i click on open in project it just opens my launch screen xib enter image description here

My appDelegate class APNS service calling part->

 func registerForPushNotifications(application: UIApplication)
{

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.sharedApplication().registerForRemoteNotifications()
            }
            else{
                CommonViewController.alertViewUI("Alert", message: "Push notification is enabled")
            }
        })
    }

    else{ //If user is not on iOS 10 use the old methods we've been using
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}

My App functioning-> On launch-----------------------

A version check class to know the user version.From there its redirected to the main(Home page).

On Home page -> 1. Load the views. 2. Calls a link asynchronously and get the count of available notification to be displayed near alerts symbol.(I am pretty sure there is no error while calling this link or getting the notification)-------------------------

Note: ** When double tapping the iPhone home button menu the app is shown in background as a opened screen with home page open(After it crashed).

**One 10.3.1 device works properly

**If the app is reinstalled everything works fine in all.

4 Answers

For me it was setting the delegate of UNUserNotificationCenter.current() before requesting authorisation that caused the crash. This is how I fixed it. Hope its helps someone else.

func registerForPushNotifications() {
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.sound, .alert, .badge]) { [weak self] granted, _ in
        print("Permission granted: \(granted)")
        guard granted else { return }
        center.delegate = self
        self?.getNotificationSettings()
    }
}

func getNotificationSettings() {
  UNUserNotificationCenter.current().getNotificationSettings { settings in
    print("Notification settings: \(settings)")
      guard settings.authorizationStatus == .authorized else { return }
      DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
      }
  }
}
Related