0

I am using gcm to push notifications to one or multiple devices, However I constantly get the error message: "mismatched sender ID".

Here is my code:

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);
    }
}

Also, when I used jGcmData.put("to","/topics/foo-bar");instead of jGcmData.put("to","token ID");, the notification can be sent successfully. However what I want is to push notification to selected devices.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31

1 Answers1

0

For the mismatched sender ID:

Try uninstalling the app and run it again.This will clear any created keys of the App.

error:MismatchSenderId

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 to the client app. If you switch to a different sender, the existing registration tokens won't work.

According to this SO answer, ""mismatchSenderId happens because the app within the same device have logged with different keys.""

For the Topic Subscription / Topic Sending

This may be related to this Subscribe to topics suddenly throws "java.io.IOException: InternalServerError", it says that "We identified an issue in our backed that affected a small percentage of the topic subscriptions during the last 24 hours. The issue has already been fixed, and the subscriptions should work correctly on all devices."

I hope this helps you.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91