Cloud messaging handing terminate app

ぐ巨炮叔叔 提交于 2019-11-28 21:15:38

问题


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


回答1:


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



回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/37333177/cloud-messaging-handing-terminate-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!