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
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.
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.