来自 Google FCM 的 MismatchSenderId

时间:2021-04-27 09:55:18

标签: android firebase-cloud-messaging

我已将我的 Android 应用程序连接到 Firebase 并在应用程序根文件夹文件 google-service.json 中看到 我已经创建了后台服务 MessagingService 并添加到 AndroidManifest.xml 服务有普通模板:

class MessagingService : FirebaseMessagingService()
{
     private NotificationManager notificationManager;

    override fun onNewToken(token: String)
    {
        super.onNewToken(token)
        if (!TextUtils.isEmpty(token) && Preferences.hasUserContext(this@MessagingService))
        {
            val user = Preferences.getUserContext(this@MessagingService)
            if (user != null)
            {
                // send token to server
                subscribeForPush(this@MessagingService, user.id!!, token)
            }
        }
    }
    companion object
    {
        @JvmStatic
        fun retrieveTokenAndSubscribeForPush(context: Context)
        {
            FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener { task: Task<InstanceIdResult?> ->
                if (!task.isSuccessful || task.result == null)
                {
                    return@addOnCompleteListener
                }
                val token = task.result!!.token
                if (!TextUtils.isEmpty(token) && Preferences.hasUserContext(context))
                {
                    val user = Preferences.getUserContext(context)
                    if (user != null)
                    {
                        // send token to server
                        subscribeForPush(context, user.id!!, token)
                    }
                }
            }
        }
...

订阅后我有这样的令牌 el2mMmTIRgOVxIN5gCNfuK:APA91bHXmj43evM2Xz76MoFDULGVmGCHyS6lIogIOa8Ph9ZqGwuRU3nYZNY4YDQkGI5Nc-SBaOVkNdHIWaxc4ABCF-T6_3OFDULGVmGCHyS6lIogIOa8Ph9ZqGwuRU3nYZNY4YDQkGI5Nc-SBaOVkNdHIWaxc4ABCF-T6_3OBUGVmGCHyS6lIogIOa8Ph9ZqGwuRU3

我还在 Firease 控制台中看到了我的 ServerKey,例如 AAAAiMkLYeI:APA91bG7-xxxxxxxxxxxxxxxxxxxxxxxxxxxxzQfpApUwEzg9v​​GeH9uPJkFQUNowjlLiT1BVyd_aTnQU_PYA4jY1c5gAOe-f-e6iW9l7BQmSs3ZHk7pIo

我尝试从作曲家接收 POST 消息到 https://fcm.googleapis.com/fcm/send 带标题

Content-Type: application/json
Authorization: key=AAAAiMkLYeI:APA91bG7-xxxxxxxxxxxxxxxxxxxxxxxxxxxxzQfpApUwEzg9vGeH9uPJkFQUNowjlLiT1BVyd_aTnQU_PYA4jY1c5gAOe-f-e6iW9l7BQmSs3ZSH7mOrKLk_kppDao88O7I4OIMp
Host: fcm.googleapis.com
Content-Length: 255

和身体

{
  "data":{
          "Trip_ID":"91505106-3F33-4508-A778-F7161949687E"
   },
  "to":"fvXclPSrROiOquWjdueMCx:APA91bFfD2y-s4vQVFCuJFiYQUc0Lym_6zR4krFumx3GLIzzuT4ST6eyWwQmJmSqZCpSr5QTNhQQPPQ99bgYp5IgiDIbP0OtrX2d8XIpjdE92rmo5NUpHfr4YEDHS6fvzNRg6Y9tm7uh"
}

但是收到 - "results":[{"error":"MismatchSenderId"}]

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,需要在“TO”字段中设置正确的数据。例如,如果订阅看起来像

FirebaseMessaging.getInstance().subsribeToTopic("weather")

正确调用 FCM 的样子

enter image description here

相关问题