2012-12-19 14 views
12

Ho seguito la demo fornita al http://developer.android.com/google/gcm/gs.html e ho ottenuto un'applicazione di prova, GCMTrial per funzionare correttamente.Errore GCM Android. Il server restituisce Non registrato quando il client è registrato

Ma, ho provato a seguire gli stessi passi su un'applicazione esistente ma non ha funzionato. Quindi ho fatto un progetto completamente nuovo. Ma anche allora, seguendo gli stessi identici passaggi, non ho potuto ottenere che GCM inviasse un messaggio con successo. Così ho provato a rinominare GCMTrial con il nome richiesto, e non anche quello non funziona.

mi iscrivo per GCM tramite l'attività principale e ottenere il seguente registro:

12-19 21:30:13.102: V/GCMRegistrar(15889): Registering receiver 
12-19 21:30:13.112: D/GCMBaseIntentService(15889): handleRegistration: registrationId =   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, error = null, unregistered = null 
12-19 21:30:13.112: D/GCMRegistrar(15889): resetting backoff for com.XXX.XXX 
12-19 21:30:13.117: V/GCMRegistrar(15889): Saving regId on app version 1 

ma quando provo ad inviare un messaggio GCM, si restituisce il seguente errore

[ errorCode=NotRegistered ] 

Codice Cliente:

public class MainActivity extends Activity { 
TextView tve; 
String TAG = "GCMTrial"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    GCMRegistrar.checkDevice(this); 
    GCMRegistrar.checkManifest(this); 
    final String regId = GCMRegistrar.getRegistrationId(this); 
    if (regId.equals("")) { 
     GCMRegistrar.register(this, "XXXXXX"); 
     Log.v(TAG, "Reg"); 
    } else { 
     Log.v(TAG, "Already registered"); 
    } 
} 

GCMIntentService : 
public class GCMIntentService extends GCMBaseIntentService { 
@Override 
protected void onError(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 
@Override 
protected void onMessage(Context arg0, Intent arg1) { 
    // TODO Auto-generated method stub 
    Log.d("GCM", "RECIEVED A MESSAGE"); 
    // Get the data from intent and send to notificaion bar 
} 
@Override 
protected void onRegistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 
@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 

}

manifesto:

Codice

Server:

System.out.println("Sending GCM"); 
    String key = "XXX"; //Server API key taken from the site 
    clientID = "XXXXXXXXX"; // copied from the logs 
    Sender sender = new Sender(key); 
    Message message = new Message.Builder().build(); 
    Result result = sender.send(message, clientID, 1); 
    System.out.println(result.toString()); 

E mi sta facendo impazzire .. Essa mostra come registrato nel telefono Android, anche chiama il metodo onRegistered della classe GCMIntentService .. ma quando provo a inviare un messaggio, bandiere "un errore NotRegistered" ... non so cosa sto facendo di sbagliato .. per favore darmi una mano ragazzi ....

+0

Non ho idea di cosa stesse succedendo. Ma, ho reinstallato gli extra di GCM per Android, ho creato nuovamente una nuova applicazione e questa volta funziona !! :) Ho usato Risultato multicastResult = sender.send (message, devices, 5); invece di Risultato risultato = sender.send (messaggio, clientID, 1); non so se questo era il problema, ma ora funziona ...! – user1657709

+0

Penso che il motivo per cui lo stai facendo funzionare dopo la reinstallazione perché hai già rivendicato un nuovo Reg ID. – neferpitou

risposta

0

hai dimenticato di aggiungere il costruttore nel vostro GCMIntentService

aggiungere quanto segue al vostro GCMIntentService

public GCMIntentService(){ 
     super(SENDER_ID); 
    } 

SENDER_ID è il XXXXXXX utilizzato per registrare la tua app.

+0

Ho provato che ... Il server continua a inviare un errore "NotRegistered" ..... Altri suggerimenti ???? – user1657709

+0

Nel mio caso, ho aggiunto il sender_ID che mi ha fornito da google alla richiesta inviata dal server. Ma ho usato ASP.Net per inviare la richiesta. –

+0

Non è SENDER_ID, è il nome di THREAD e deve essere SEND_ID, potrebbe essere qualsiasi stringa. – neferpitou

11

Bene, so che si tratta di un bug corrente durante l'installazione con adb.

GCMRegistrar non sa che il dispositivo non è registrato sul server GCM e non si registra.

Che cosa si può fare è di registrare in ogni caso:

if (regId.equals("")) { 
     GCMRegistrar.register(this, "XXXXXX"); 
     Log.v(TAG, "Reg"); 
    } else { 
     //I SAY BULLSHIT ! 
     GCMRegistrar.register(this, "XXXXXX"); 
     Log.v(TAG, "Reg"); 
    } 

O uninstal l'applicazione sul client quindi eseguire una nuova installazione da Adb.

Tenere a mente che ci sono poche possibilità che ciò accada nella produzione.

0

È possibile provare a sostituire il contesto "questo" con "getApplicationContext()" per utilizzare il contesto dell'applicazione. Ecco il mio codice di esempio:

private void registerGCM() { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { 
     //prevent crash caused by no google account or not support for gcm 
     try { 
      GCMRegistrar.checkDevice(this); 
     } catch (Exception e) { 
      Log.i("test", "!!!cgm " + e.toString()); 
      return; 
     } 
    } 

    final String regId = GCMRegistrar.getRegistrationId(this); 
    if (TextUtils.isEmpty(regId)) { 
     String gcmSenderId = PreferencesUtil.getStringPreference(PreferencesUtil.SETTING_GCM_SENDER_ID, 
       "", getApplicationContext()); 
     GCMRegistrar.register(getApplicationContext(), gcmSenderId); 
    } 
    mGCMRegisted = true; 
} 
Problemi correlati