gcmsender error: MismatchSenderId

gcmsender error: MismatchSenderId

我正在使用示例应用程序测试更新后的 google gsm 云服务 https://github.com/googlesamples/google-services/tree/master/android/gcm 我在这里关注 google 教程: https://developers.google.com/cloud-messaging/android/start

设备客户端已设置,我正在接收令牌,并且我已经在我的开发者控制台上下载并配置了 google-services.json。

我知道 gcmsender 模块使用 API 键发送测试消息,但是我收到错误消息: "results":[{"error":"MismatchSenderId"}]} 我已确认 google-services.json 使用的发件人 ID 与开发人员控制台提供给我的发件人 ID 相同。 我错过了什么?

基于 Official Google Documentation. You will receive 'error:MismatchSenderId' when a registration token is tied to a certain group of senders. When a client app registers for GCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages 到客户端应用程序。如果您切换到不同的发件人,现有的注册令牌将无法使用。 检查 Sender IDAPI_KEY,API 密钥在 Google Developer Console 中生成并用于authenticate/authorize 与 GCM 和发件人 ID 被 Android 应用程序用于向 GCM 注册物理设备以便能够接收通知。确保 API 键属于 'Key for server apps'

请在您的终端运行下面的脚本

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

MismatchSenderId 因为在同一台设备上您使用不同的密钥登录。要解决此问题,请卸载应用程序并 运行 再次更新注册码。然后 运行 终端中的 CURL 脚本,我 post 上面会给出成功消息,你会收到通知到你的设备

如果您使用 cordova 插件 phonegap-plugin-push 那么很可能您在 npm package.json 中定义了您的发件人 ID,如下所示:

{
  "id": "phonegap-plugin-push",
  "locator": "phonegap-plugin-push",
  "variables": {
    // TODO: this sender ID should probably be moved into a config file that can be inserted by a deploy script
    "SENDER_ID": "2342343443"
  }
},
"cordova-plugin-whitelist",
"cordova-plugin-console",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard",
"ionic-plugin-deploy"

...

如果您没有设置发件人 ID,或者当您在后端更改 Google API 控制台项目而忘记更新应用程序的前端部分时,就会发生错误。

Google API 控制台中的发件人 ID 隐藏得很好。转到此处并向下滚动到更新的答案:Android GCM SENDER_ID, how to get it?

在这两个位置,发件人 ID 需要相同。不要忘记重新部署/重新安装您的应用程序。