Android 为什么我从 GCM 服务器端得到“MismatchSenderId”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11313342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:53:51  来源:igfitidea点击:

Why do I get "MismatchSenderId" from GCM server side?

androidpush-notificationgoogle-cloud-messaging

提问by Seyed Vahid Hashemi

I'm trying to create a push service for my Android app, and I follow Google GCM's documentation and example for this matter:

我正在尝试为我的 Android 应用程序创建一个推送服务,我遵循 Google GCM 的文档和示例来解决这个问题:

I can register/unregister my Android app. From my server side, I can see that I have one subscription registered, but when I try to send a message from server to my Android app I always get following error:

我可以注册/取消注册我的 Android 应用程序。从我的服务器端,我可以看到我注册了一个订阅,但是当我尝试从服务器向我的 Android 应用程序发送消息时,我总是收到以下错误:

Error sending message to device #0: MismatchSenderId

向设备 #0 发送消息时出错:MismatchSenderId

For my Android app, I use SENDER_ID = 200000000001.

对于我的 Android 应用程序,我使用 SENDER_ID = 200000000001。

And for my server side as Google says I use API_KEY = AIzxxxxxxxxxxxxxxxxXxxXxxXxxxXXXXXxxxxs

对于我的服务器端,正如谷歌所说,我使用 API_KEY = AIzxxxxxxxxxxxxxxxxXxxXxxXxxxXXXXXXxxxxs

I followed this document:

我遵循了这个文件:

http://developer.android.com/guide/google/gcm/demo.html

http://developer.android.com/guide/google/gcm/demo.html

I've enabled GCM in my Google API panel too, yet I'll get that annoying error message.

我也在我的 Google API 面板中启用了 GCM,但我会收到那条烦人的错误消息。

How can I fix this problem?

我该如何解决这个问题?

采纳答案by azgolfer

Did your server use the new registration ID returned by the GCM server to your app? I had this problem, if trying to send a message to registration IDs that are given out by the old C2DM server.

您的服务器是否使用了 GCM 服务器返回给您的应用程序的新注册 ID?如果尝试向旧 C2DM 服务器提供的注册 ID 发送消息,我遇到了这个问题。

And also double check the Sender ID and API_KEY, they must match or else you will get that MismatchSenderId error. In the Google API Console, look at the URL of your project:

还要仔细检查发件人 ID 和 API_KEY,它们必须匹配,否则您将收到 MismatchSenderId 错误。在 Google API 控制台中,查看项目的 URL:

https://code.google.com/apis/console/#project:xxxxxxxxxxx

The xxxxxxxxx is the project ID, which is the sender ID.

xxxxxxxxx 是项目 ID,即发送方 ID。

And make sure the API Key belongs to 'Key for server apps (with IP locking)'

并确保 API 密钥属于“服务器应用程序密钥(带 IP 锁定)”

回答by Ajay Singh

Mismatch happens when you don't use the numeric ID. Project ID IS NOT SENDER ID!! It took me 9 hours to figure this out. For all the confusion created by google, check the following link to get numeric id.

不使用数字 ID 时会发生不匹配。项目 ID 不是发件人 ID!我花了 9 个小时才弄明白。对于谷歌造成的所有混淆,请查看以下链接以获取数字 ID。

https://console.cloud.google.com

https://console.cloud.google.com

instead of

代替

https://console.developers.google.com

https://console.developers.google.com

Hope it helps!!

希望能帮助到你!!

Update:- Things have changed again. Now the sender id is with firebase.

更新:-事情又发生了变化。现在发件人 ID 带有 firebase。

Go to https://console.firebase.google.comand select your project. Under settings -> cloud messaging, you can find the sender id.

转到https://console.firebase.google.com并选择您的项目。在设置 -> 云消息传递下,您可以找到发件人 ID。

And it works!

它有效!

回答by Raj Kumar

Please run below script in your terminal

请在终端中运行以下脚本

curl -X POST \
-H "Authorization: key=  write here api_key" \
-H "Content-Type: application/json" \
-d '{ 
"registration_ids": [ 
"write here reg_id generated by gcm"
], 
"data": { 
"message": "Manual push notification from Rajkumar"
},
"priority": "high"
}' \
https://android.googleapis.com/gcm/send

it will give the message if it is succeeded or failed

如果成功或失败,它将给出消息

回答by mpavel

I encountered the same issue recently and I tried different values for "gcm_sender_id" based on the project ID. However, the "gcm_sender_id" value must be set to the "Project Number".

我最近遇到了同样的问题,我根据项目 ID 为“gcm_sender_id”尝试了不同的值。但是,“gcm_sender_id”值必须设置为“项目编号”。

You can find this value under: Menu > IAM & Admin > Settings.

您可以在以下位置找到此值:菜单 > IAM 和管理 > 设置。

See screenshot: GCM Project Number

见截图:GCM 项目编号

回答by Ivan Vizuet

InstanceID.getInstance(getApplicationContext()).getToken(authorizedEntity,scope)

authorizedEntity is the project number of the server

authorizedEntity 是服务器的项目号

回答by Britc

Your android app needs to correct 12-digit number id (aka GCM Project Number). If this 12-digit number is incorrect, then you will also get this error.

您的 android 应用需要更正 12 位数字 ID(又名 GCM 项目编号)。如果这个 12 位数字不正确,那么您也会收到此错误。

This 12-digit number is found in your Google Play Console under your specific app, 'Service & API' -> 'LINKED SENDER IDS'

这个 12 位数字可在您的 Google Play Console 中的特定应用下找到,“服务和 API”->“已链接的发件人 IDS”

回答by Chamath Jeevan

This happens when the Server keyand Sender IDparameters HTTP request do not match each other. Basically both server ID and Server key must belong to the same firebase project. Please refer to the below image. In case of mixing these parameters from deferent Firebase projects will cause error MismatchSenderIdenter image description here

服务器密钥发件人 ID参数 HTTP 请求彼此不匹配时,就会发生这种情况 。基本上服务器 ID 和服务器密钥都必须属于同一个 firebase 项目。请参考下图。如果混合来自不同 Firebase 项目的这些参数将导致错误MismatchSenderId在此处输入图片说明

回答by CoolMind

Check google-services.jsonfile in appfolder of your Android project. Generate a new one from Firebase console if you are unsure. I got this error in two cases.

检查Android 项目google-services.json文件app夹中的文件。如果您不确定,请从 Firebase 控制台生成一个新的。我在两种情况下遇到此错误。

  1. I used a test Firebase project with test application (that contained right google-services.jsonfile). Then I tried to send push notification to another application and got this error ('"error": "MismatchSenderId"'). I understood that the second application was bound to another Firebase project with different google-services.json. Because server keys are different, the request should be rewritten.

  2. I changed google-services.jsonin the application, because I wanted to replace testFirebase project with an actual. I generated right google-services.jsonfile, changed request, but kept receiving this error. On the next day it fixed itself. I suspect Firebase doesn't update synchronously.

  1. 我使用了一个带有测试应用程序的测试 Firebase 项目(包含正确的google-services.json文件)。然后我尝试向另一个应用程序发送推送通知并收到此错误('"error": "MismatchSenderId"')。我知道第二个应用程序绑定到另一个具有不同google-services.json. 因为服务器密钥不同,所以应该重写请求。

  2. 我改变了google-services.json应用程序,因为我想替换测试与火力地堡项目实际。我生成了正确的google-services.json文件,更改了请求,但一直收到此错误。第二天就自己修好了。我怀疑 Firebase 不会同步更新。

To get a server key for the request, open https://console.firebase.google.comand select an appropriate project.

要获取请求的服务器密钥,请打开https://console.firebase.google.com并选择适当的项目。

enter image description here

在此处输入图片说明

Then paste it in the request.

然后将其粘贴到请求中。

enter image description here

在此处输入图片说明

回答by Riefvan

Use sender ID & API Key generated here: http://developers.google.cominstead (browse for Google Cloud Messaging first and follow the instruction).

使用此处生成的发件人 ID 和 API 密钥:http: //developers.google.com(首先浏览 Google Cloud Messaging 并按照说明进行操作)。

回答by Iman Marashi

If use for native Android, check your AndroidMaifest.xmlfile:

如果用于原生 Android,请检查您的AndroidMaifest.xml文件:

<meta-data
        android:name="onesignal_google_project_number"
        android:value="str:1234567890" />  
  <!--             its is correct. -->

instead

反而

   <meta-data
        android:name="onesignal_google_project_number"
        android:value="@string/google_project_number" />

Hope it helps!!

希望能帮助到你!!