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)")
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)
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With