Я использую gcm для отправки уведомлений на одно или несколько устройств. Однако я постоянно получаю сообщение об ошибке: «несоответствующий идентификатор отправителя».Google может сообщить «ошибка»: «MismatchSenderId»?
Вот мой код:
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");
, уведомление может быть отправлено успешно. Однако то, что я хочу, - это нажать уведомление выбранным устройствам.