4

This questions has been asked and answered many times before, yet I can't figure out what I am doing wrong. Sending notifications to the whole app from Firebase console works, but if I send to a single token, I get error in firebase console: "Firebase Invalid registration token. Check the token format"

I am testing the app on an Iphone device.

NOTE: In developer.apple.com, under APP IDs, if I click on myApp id and scroll down, Push Notifications Configurable & Development show in yellow color and not green.

Xcode version Version 8.3.3 (8E3004b), Swift 3.0
pod file    
pod 'Firebase', '~> 3.9.0'
pod 'Firebase/Auth', '~> 3.9.0'
pod 'Firebase/Database', '~> 3.9.0'
pod 'Firebase/Storage'
pod 'Firebase/Messaging'

Using Firebase (3.9.0)
Using FirebaseAnalytics (3.5.1)
Using FirebaseAuth (3.0.6)
Using FirebaseCore (3.4.4)
Using FirebaseDatabase (3.1.0)
Using FirebaseInstanceID (1.0.9)
Using FirebaseMessaging (1.2.1)
Using FirebaseStorage (1.0.4)
Using GTMSessionFetcher (1.1.12)
Using GoogleInterchangeUtilities (1.2.2)
Using GoogleSymbolUtilities (1.1.2)
Using GoogleToolboxForMac (2.1.3)



import UIKit
import Firebase
import FirebaseCore
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications

 @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

 var window: UIWindow?


  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  FIRApp.configure()

  NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(notification:)), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)

  //obtain the user’s permission to show any kind of notification
  registerForPushNotifications()
  return true
}//end of didFinishLaunchingWithOptions


 //obtain the user’s permission to show any kind of notification
 func registerForPushNotifications() {

  // iOS 10 support
  if #available(iOS 10, *) {
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
        print("Permission granted: \(granted)")

        guard granted else {return}
        self.getNotificationSettings()
    }
}
    // iOS 9 support
 else if #available(iOS 9, *){UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    self.getNotificationSettings()
}
    // iOS 8 support
  else if #available(iOS 8, *) {UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    self.getNotificationSettings()
 }
    // iOS 7 support
else {
    UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
 }
}//end of registerForPushNotifications()



  //if user decliens permission when we request authorization to show notification
  func getNotificationSettings() {
 UNUserNotificationCenter.current().getNotificationSettings { (settings) in

     print("settings.authorizationStatus is \(settings.authorizationStatus)")
    guard settings.authorizationStatus == .authorized else {return}
    UIApplication.shared.registerForRemoteNotifications()
   }
 }


func tokenRefreshNotification(notification: NSNotification) {
 let refereshToken = FIRInstanceID.instanceID().token()
 print("instance ID token is \(refereshToken)")
 prints: c-_B0W1AKX0:APA91bHBCtFhGtteH1r2y7c8gpUJpfrgDZYtncFmxZQht_wBDWk9Stdf78aMqUctKYU_OlIkmMNW-KLP68_IhdZCM2WxcN4fU1XkoIVNCGTvBogzSpgt4IkveLbK7rNX7pQTfmP72MfV

 connectToFcm()
}


 func connectToFcm() {
   FIRMessaging.messaging().connect { (error) in
      if error != nil  {
         print("unable to connect to FCM")
     }else {
         print("connected to FCM")
     }
   }
 }

}//end of AppDelegate    

enter image description here enter image description here enter image description here enter image description here enter image description here

bibscy
  • 2,598
  • 4
  • 34
  • 82
  • Did you include this code on how to access the registration token? https://firebase.google.com/docs/cloud-messaging/ios/client#access_the_registration_token – Jen Person May 15 '18 at 22:16
  • The token looks fine and valid. Strange. Does this occur for all iOS versions? Have you tried generating one for a different version yet? – AL. May 16 '18 at 01:12
  • AL. I tried Version 10.3 and 9.3. Jen Person the code at that link suggested is for GCM not FCM. The code that you see in this question is what is running in my project. – bibscy May 16 '18 at 07:43
  • You should configure the push notification for development mode. For that, you have to create the CSR file. – Ajay Chaudhary May 21 '18 at 18:30
  • @AjayChaudhary I am using Key which is valid both for production and development. developer.apple.com/account/ios/authkey/create . APNs Authentication Key is the preferred way as it Never expires –https://stackoverflow.com/a/39703429/5918849 – bibscy May 21 '18 at 19:17
  • hmm, in this case, you have to deal with .pem files, not with .p8 files because FCM is not allowing to upload the .p8 file – Ajay Chaudhary May 22 '18 at 10:21
  • or second option is, you have to manage the notification from your own server instead of FCM. – Ajay Chaudhary May 22 '18 at 10:21
  • @AL. could you please tell me what is wrong with the request I am making? I am sure it's a small detail I am missing https://stackoverflow.com/questions/50486869 – bibscy May 23 '18 at 14:43

1 Answers1

2

When I created the app in Firebase console, I mistyped a letter when filling in the Bundle ID.
I never thought about checking it since, I had been using Firebase Database services and all CRUD operations worked as expected, but the Bundle ID in Xcworkspace and BundleID of the firebase app were not exactly the same.
It would be great if anyone from Google could say the reason why reading/writing from the client app worked as expected even though the bundleID was incorrect in Firebase console.

How did I find out the typo?
Initially I uploaded in Firebase console the p8 file which was downloaded from developer.apple.com and no error was shown.
Later on, I decided to use certificates (the old way) and when I tried to upload Apple Push Notification service SSL (Sandbox) - Development I got an error The certificate bundleId did not match that of your app

bibscy
  • 2,598
  • 4
  • 34
  • 82
  • I think I'm getting a similar issue, mind talking more about how you solved the issue with more specific steps? Thanks! – mding5692 Oct 05 '18 at 22:06
  • 1
    go to your project root directlory, then click on General, look at Bundle Identifier: "com.mytem.newGreatApp.com" . Then go to your firbase project, at the top left corner of the page click on project settings, then click on General, scroll to the bottom of the page and check if the Bundle Identifier in your Xcode project is the same with the bundle identifier in at the bottom of the General page. You can also delete from Xcode GoogleService-Info.plist file and download it again. – bibscy Oct 07 '18 at 10:06
  • Actually I figured it out, it was a stupid mistake, my APNs certificates expired – mding5692 Oct 08 '18 at 18:17