【问题标题】:google could message-"error":"MismatchSenderId"?谷歌可以消息-“错误”:“MismatchSenderId”?
【发布时间】:2016-05-19 20:35:08
【问题描述】:

我正在使用 gcm 将通知推送到一台或多台设备,但是我经常收到错误消息:“发件人 ID 不匹配”。

这是我的代码:

public static void post(String apiKey){
    try{
        // prepare JSON
        JSONObject jGcmData = new JSONObject();
        JSONObject jData = new JSONObject();

        jData.put("message", "{good luck}");

        jGcmData.put("to","token ID");

        jGcmData.put("data", jData);



        // Create connection to send GCM Message request.
        URL url = new URL("https://android.googleapis.com/gcm/send");


        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + apiKey);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send GCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(jGcmData.toString().getBytes());

        // Read GCM response.
        InputStream inputStream = conn.getInputStream();
        String resp = IOUtils.toString(inputStream);
        System.out.println(resp);
    } catch (IOException e) {
        System.out.println("Unable to send GCM message. "+e);
    }
}

另外,当我使用jGcmData.put("to","/topics/foo-bar");而不是jGcmData.put("to","token ID");时,可以成功发送通知。但是,我想要将通知推送到选定的设备。

【问题讨论】:

    标签: java server google-cloud-messaging


    【解决方案1】:

    对于不匹配的发件人 ID

    尝试卸载应用程序并再次运行它。这将清除应用程序的所有已创建密钥。

    error:MismatchSenderId

    注册令牌与特定的发件人组相关联。当客户端应用注册 GCM 时,它必须指定允许哪些发件人发送消息。在向客户端应用程序发送消息时,您应该使用其中一个发件人 ID。如果您切换到其他发件人,现有的注册令牌将不起作用。

    根据SO answer""mismatchSenderId 发生是因为同一设备中的应用使用不同的键登录。""

    主题订阅/主题发送

    这可能与此Subscribe to topics suddenly throws "java.io.IOException: InternalServerError" 有关,它说 “我们发现了一个问题,在过去 24 小时内影响了一小部分主题订阅。该问题已得到修复,并且订阅应该在所有设备上都能正常工作。”

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      相关资源
      最近更新 更多