Felipe Parra Felipe Parra - 1 year ago 2534
iOS Question

Cloud messaging handing terminate app

I have an application that stores the user's session in NSUserDefaults. When the application is forced to close, in the initial verify whether the data controller user session there, in case if there sent it to the start window as follows:

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)

self.view.hidden = false



This worked smoothly me until today when I decided to implement push notifications with updating firebase following this tutorial
Setting up a Firebase Cloud Messaging Client App on iOS
. The problem occurs when he killed the application and enter again gives the following error code:

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)")

Answer Source

Here is the solution,

First Upload the necessary certificates in Firebase Console Then in your app enable Push Notifications and Background Modes -> Remote Notifications

After that in App Delegate use the code below(I specify the tricky line) :

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

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

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

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download