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.
Most often, apps crash because they aren't updated and have unaddressed bugs. To fix this, go to the Updates section of the App Store and update the app if an update is available.
You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.
Open the Console app, from Applications > Utilities in Finder. Select Crash Reports. Locate crash reports for your app in the list. Logs are listed by your app's binary name.
Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.
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.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With