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

4

8

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.

Proteinase answered 12/5, 2017 at 5:39 Comment(2)
Change the scheme of xcode project from automatic to manual and send push notification and debug to exactly understand what is happening exactlySubtonic
You need to post the code for receiving a push notification as well as the didFinishLaunching method since that's where you say the crash is occurring. The posted code is for registering for push notifications.Ahlgren
D
4

If I understand you correct, your app is moving to the background and crashes when coming back to foreground.

This doesn't look like an issue with UNUserNotifications. More like the notification just triggers the crash in your app in that case. Stack shows the crash happens in your app. That means you are running on a null pointer reference somewhere when returning to the foreground.
Blind guess:
Are you listening to UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification?
If yes, each class that listens to those notifications needs unsubscribe before they meet the garbage collector. Good place for that is dealloc/deinit.

Since you use Swift:

deinit {
    NotificationCenter.default.removeObserver(self)
}

Objective-C ARC would look like:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

If its not listening to notifications, then it is still a reference somewhere that get deallocated. The reason it doesn't crash right away would be then a simple Zombie. An object instance that is about to get deallocated, but didn't yet during runtime on some devices.

Deliquescence answered 15/5, 2017 at 11:55 Comment(0)
G
1
  • This code resolve your problem::-

    func registerPushNotifications() {
    
     if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
    }
    
Goodell answered 15/5, 2017 at 11:1 Comment(0)
I
0

Please check that what you have written in this because this function is called on receiving push notification. Please debug this.

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { }

Ieper answered 15/5, 2017 at 9:44 Comment(0)
S
0

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()
      }
  }
}
Scrivner answered 9/5, 2022 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.