Android GCM error. Server returns Not Registered when client IS registered

人走茶凉 提交于 2019-12-23 07:07:09

问题


I followed the demo given at http://developer.android.com/google/gcm/gs.html and got a trial application, GCMTrial to work correctly.

But, i tried to follow the same steps on an existing application but it didnot work. So I made an entirely new project. But even then , following the same exact steps, I couldnt get GCM to send a message successfully. So I tried renaming GCMTrial to the required name, and not that too does not work.

I register for GCM via the main activity and get the following log :

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

But when i try to send a GCM message, it returns the following error

[ errorCode=NotRegistered ]

Client Code :

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
}

}

Manifest :

Server Code :

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

It is driving me crazy.. It shows as registered in the android phone, even calls the onRegistered method of the GCMIntentService class.. but when i try to send a message, it flags a "NotRegistered Error" ... I dont know what im doing wrong.. please help me out guys....


回答1:


Well, know it's a current bug when installing with adb.

GCMRegistrar does not know that the device is unregistered on GCM server and don't register.

What you can do is to register anyway :

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

Or uninstal the app on client then run a new install from Adb.

Keep in mind there is few chances it happens in production.




回答2:


you forgot to add the constructor in your GCMIntentService

add the following to your GCMIntentService

public GCMIntentService(){
        super(SENDER_ID);
    }

SENDER_ID is the XXXXXXX that used to register your app.




回答3:


You can try to replace the context "this" to "getApplicationContext()" to use the application context. Here is my sample code:

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


来源:https://stackoverflow.com/questions/13956677/android-gcm-error-server-returns-not-registered-when-client-is-registered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!