Cloud messaging handing terminate app
Asked Answered
S

4

20

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


    }else
    {
    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)")

Screenshot

Spectral answered 19/5, 2016 at 20:15 Comment(2)
have you correctly initialized the general firebase framework?Bluey
Yes, my AppDelegate is identical to that of this example Firebase Messaging Quickstart . I receive notifications correctly, the problem is when I kill the application and reopens a crash occurs, then retry and operating normally.Spectral
G
35

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 {
    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)
}
Gondi answered 19/5, 2016 at 22:19 Comment(2)
This solves the problem, no longer gives the error. Now as I can control the download of data when the application is in the background?Spectral
@Gondi ..I am also facing same issue even now.Cockswain
A
5

Do not forgot in AppDelegate:

import Firebase
import FirebaseInstanceID
import FirebaseMessaging
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    registerForPushNotifications(application)
    FIRApp.configure()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter
     .defaultCenter()
     .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                                                     name: kFIRInstanceIDTokenRefreshNotification, object: nil)

    // Override point for customization after application launch.
    return true
  }

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


  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                   fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("===== didReceiveRemoteNotification ===== %@", userInfo)
  }


 func tokenRefreshNotificaiton(notification: NSNotification) {
    let refreshedToken = FIRInstanceID.instanceID().token()!
    print("InstanceID token: \(refreshedToken)")

    // Connect to FCM since connection may have failed when attempted before having a token.
     connectToFcm()
  }

  func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
      if (error != nil) {
        print("Unable to connect with FCM. \(error)")
      } else {
        print("Connected to FCM.")
      }
    }
  }

Please make sure also to do in Info.plist : FirebaseAppDelegateProxyEnabled = NO

I don't know for now but I got the print(...) in didReceiveRemoteNotification but don't get the popup. I send the message from Firebase -> Console -> Notification -> Single device and copy here the token which I got from Xcode Console -> func tokenRefreshNotificaiton

Aaronaaronic answered 25/5, 2016 at 22:46 Comment(2)
missing piece: .addObserver, saved me!Epitomize
@Mr.Bista. Please guide me about push notification ,working with FCM .I am able to data message from server but notification method is not calling.Please check my question #41199151Cockswain
A
4

Answer is correct those are the steps , But also please check your device time . Since if your time and date is way too off it wont work

Apposite answered 29/7, 2016 at 2:52 Comment(0)
L
2

After having tripple checked the whole integration, for me the issue was that the test device had the date changed to a week in advance. Probably the FCM SDK does some date based checks.

The error could be less generic on the Firebase side, since I lost almost a day searching for a solution. Hope this helps.

Larch answered 25/10, 2016 at 11:57 Comment(1)
Thank you so much, this was exactly my problem as well!Izmir

© 2022 - 2024 — McMap. All rights reserved.