当前位置: 首页 > 知识库问答 >
问题:

Firebase注册令牌无效。检查令牌格式

姚宪
2023-03-14

这个问题以前被问过很多次,也被回答过很多次,但是我不知道我做错了什么。从Firebase控制台向整个应用程序发送通知可以正常工作,但如果我向单个令牌发送通知,则Firebase控制台会出现错误:"Firebase注册令牌无效。检查令牌格式”

我正在Iphone设备上测试该应用程序。

  • 已在创建新密钥https://developer.apple.com/account/ios/authkey/ 下载了p8文件

注意:在developer.apple.com的APP id下,如果我点击myApp id并向下滚动,就会推送通知

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    

共有1个答案

呼延哲
2023-03-14

当我在Firebase控制台中创建应用程序时,我在填写捆绑包ID时输入了一封错误的信。
我从未想过检查它,因为我一直在使用Firebase数据库服务,所有CRUD操作都按预期工作,但是Xcworkspace中的Bundle ID和firebase应用程序的Bundle ID并不完全相同。
如果谷歌的任何人都能说出为什么从客户端应用程序读取/写入数据能按预期工作,那就太好了,即使firebase控制台中的Bundle ID不正确。

我是如何发现输入错误的?
最初我在Firebase控制台上传了从developer.apple.com下载的p8文件,没有显示任何错误。
后来,我决定使用证书(旧方法),当我尝试上传苹果推送通知服务SSL(沙盒)时-开发我收到一个错误,证书绑定ID与应用程序的不匹配

 类似资料:
  • 是否有一种方法可以在我的服务器上验证Firebase注册令牌,而无需往返Firebase服务器?我只是想知道它们是否有效,不一定是有效的。

  • 问题内容: 尝试为变量分配值时出现问题。当我尝试按以下顺序将日期作为元组或列表时,问题就会出现: 为什么要打apping? 我如何解决它? 令牌在Python中是什么意思? 问题答案: 在Python 3中,数字前导零是不允许的。例如: 等是不允许的,但应改为和。 但是,在Python 2中,前导零表示该数字是一个 八进制数 (以8为底),因此,或分别表示和,并且以八进制表示,但由于它不是有效的八

  • 问题内容: 对于我正在研究的一个新的node.js项目,我正在考虑从基于cookie的会话方法切换(这意味着,将ID存储到包含用户会话的键值存储中的用户浏览器中)到使用JSON Web令牌(jwt)的基于令牌的会话方法(无键值存储)。 该项目是一个利用socket.io的游戏-在单个会话(web和socket.io)中会有多个通信渠道的情况下,基于令牌的会话将非常有用。 如何使用jwt方法从服务器

  • 对于我正在进行的一个新的Node.js项目,我正在考虑从基于cookie的会话方法(我的意思是,将id存储到包含用户浏览器中用户会话的键值存储)切换到使用JSON Web令牌(jwt)的基于令牌的会话方法(无键值存储)。 这个项目是一个利用socket.io的游戏--在一个会话中有多个通信通道(web和socket.io)的情况下,拥有一个基于令牌的会话会很有用 如何使用jwt方法从服务器提供令牌

  • 问题内容: 我正在尝试使用OAuth 2.0访问Google的文档列表API 3.0,但是遇到401错误的麻烦。 用户接受后,我的代码如下: 然后,在最后一行-getFeed()-引发异常: 这是怎么回事?在静态主测试类上,它的工作原理很吸引人,但是当我在服务器上运行它时,此行不再起作用。任何想法? 解决了 需要使用GoogleOAuthHelper而不是直接使用GoogleOAuthParame

  • 我有一个计划的WebJob,每天运行,检查数据库中所有用户的密码过期日期。如果密码到期日是今天,它将生成密码重置令牌并通过电子邮件发送给用户。然后用户单击电子邮件中的url,并被带到一个网站,在那里输入新密码。 我设法在我的WebWork中生成一个令牌,并通过电子邮件发送。但是,通过我的Asp重置密码时。NET网站我得到无效令牌。我不明白为什么。我假设它一定与我的WebWork中的令牌提供商有关。