Sto tentando di inviare una notifica utilizzando GCM sul mio dispositivo Android, ma ottengo sempre l'errore InvalidRegistration.Utilizzo di GCM per inviare notifiche sull'app, restituisce InvalidRegistration error
Ecco il codice PHP che dovrebbe inviare la notifica:
<?php
define('API_ACCESS_KEY', 'API KEY HIDDEN');
$registrationIds = array($_GET['id']);
// prep the bundle
$msg = array
(
'message' => 'TestMessage',
'title' => 'TestTitle',
'subtitle' => 'TestSubtitle',
'tickerText' => 'TestTicker',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Questo è come la registrazione viene effettuata:
private void registerInBackground() {
new AsyncTask<Void, Object, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
//Here I save the registration ID, and when I try to use it manually, by running the PHP script above, I get the error.
sendRegistrationIdToBackend();
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(context, msg + "\n", Toast.LENGTH_SHORT).show();
}
}.execute(null, null, null);
Dopo aver eseguito l'applicazione, e chiamando il metodo che registra l'app, ottengo un ID di registrazione. Poi, quando si digita localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx
ricevo
{"multicast_id":8460288429151356251,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Ecco, ho avuto un piccolo problema, ora l'ho risolto. Ricevo 'successo: 1' ora, ho un'altra domanda, come posso iniziare a ricevere messaggi nell'app adesso? Non stanno arrivando – Val
https://developer.android.com/google/gcm/client.html controlla la parte per Ricevere un messaggio a valle – Msk
Ricevo anche un problema smilar come l'hai risolto @Val –