Jee*_*n_7 8 apple-push-notifications swift swift2
我刚刚想出了如何在我的应用中正确发送推送通知.但是当它开始正常工作时,就会出现一种新的错误.收到推送通知后,我的应用程序在启动时崩溃.我在5台设备上进行了测试,其中2台由于问题而崩溃(两台都在iOS_10.3.1上运行).奇怪的部分是在iOS 10.2和9.3.1上运行的其他3个设备.我真的不认为这是与操作系统有关的东西.
Apple发送了这样的崩溃日志,但当我点击项目中的open时,它只会打开我的启动屏幕xib

我的appDelegate类APNS服务调用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)
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序功能 - >启动时-----------------------
用于了解用户版本的版本检查类.从那里将其重定向到主(主页).
在主页 - > 1.加载视图.2.异步调用链接,并在警报符号附近显示可用通知的计数.(我很确定在调用此链接或获取通知时没有错误)------------ -------------
注意:**当双击iPhone主页按钮菜单时,应用程序在后台显示为打开的主屏幕打开的屏幕(崩溃后).
**一个10.3.1设备正常工作
**如果重新安装该应用程序,一切正常.
如果我理解正确,您的应用程序将移至后台并在返回前台时崩溃。
这看起来不像是 UNUserNotifications 的问题。在这种情况下,更像是通知只会触发应用程序崩溃。堆栈显示崩溃发生在您的应用程序中。这意味着当返回前台时,您正在某处运行空指针引用。
盲猜:
您在听 UIApplicationDidBecomeActiveNotification 还是 UIApplicationWillEnterForegroundNotification?
如果是,则每个监听这些通知的类在遇到垃圾收集器之前都需要取消订阅。比较好的地方是 dealloc/deinit。
由于您使用 Swift:
deinit {
NotificationCenter.default.removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)
Objective-C ARC 看起来像:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)
如果它没有监听通知,那么它仍然是被释放的某个地方的引用。它没有立即崩溃的原因就是一个简单的僵尸。即将被释放但在某些设备上运行时尚未释放的对象实例。