ios - 云消息处理终止应用程序

标签 ios swift firebase apple-push-notifications

我有一个将用户 session 存储在 NSUserDefaults 中的应用程序。当应用程序被强制关闭时,在初始验证数据 Controller 用户 session 是否存在,如果存在则将其发送到启动窗口如下:

override func viewWillAppear(animated: Bool) {

    self.view.hidden = true

    let defaults = NSUserDefaults.standardUserDefaults()
    if   defaults.stringForKey("user") != nil
    {

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("vistaInicio") as! ViewControllerInicio
            self.presentViewController(viewController, animated: true, completion: nil)
        })


    }else
    {
    self.view.hidden = false

    }

}

直到今天,当我决定按照本教程 Setting up a Firebase Cloud Messaging Client App on iOS 更新 firebase 来实现推送通知时,这对我来说一直很顺利。 .当他杀死应用程序并再次输入时出现问题,并给出以下错误代码:

 2016-05-19 16:05:27.647: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(full)"
 2016-05-19 16:05:27.659: <FIRMessaging/INFO> FIRMessaging library version 1.1.0
 2016-05-19 16:05:27.831: <FIRMessaging/WARNING> FIRMessaging registration is not ready with auth credentials
Unable to connect with FCM. Optional(Error Domain=com.google.fcm Code=501 "(null)")

Screenshot

最佳答案

这是解决方案,

首先在 Firebase 控制台中上传必要的证书然后在您的应用中启用推送通知和后台模式 -> 远程通知

之后在 App Delegate 中使用下面的代码(我指定了棘手的行):

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    registerForPushNotifications(application)
    // Override point for customization after application launch.
    // Use Firebase library to configure APIs
    FIRApp.configure()
    return true
}

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    //Tricky line
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
    print("Device Token:", tokenString)
}

关于ios - 云消息处理终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37333177/

相关文章:

ios - UITableViewCell 之间的间距有多高?

swift - 如何在 swift 3 中的图像上的两点之间画一条线?

firebase - 在本地托管的 Firebase 应用程序中使用本地模拟的 https.onCall Firebase 函数

swift - Firestore数组查询超过10个元素(分页)

ios - iOS-SDWebImage加载错误的图像

ios swift 显示/隐藏textView单元格中的元素并清除其空间

ios - 交叉构建 binutils 2.25.1 for iOS8.4 (arm64-apple-darwin14.0.0)

ios - 自动布局在表格 View 中不起作用

swift - 使用 Realm 和 swift xcode 的客户端-服务器方法

javascript - React-Native Firebase 使用 FlatList 显示对象数组