0
votes

I am trying to work with GCM(Google Cloud Messaging) for Push Notification, i have tried with sample application it's working fine with my device and Device Id/Token is generating and able to push notification.

If i try the same application with another device application is running but Device Token/Registration id is not getting generated.

I don't know where i'm making mistake need help to figure it out.

Thanks in advance.

Here's my code snippet

 public void onCreate(){
    super.onCreate();
    final String preferences = getString(R.string.preferences);
    savedValues = getSharedPreferences(preferences, Context.MODE_PRIVATE);
    // In later versions multi_process is no longer the default
    if(VERSION.SDK_INT >  9){
        savedValues = getSharedPreferences(preferences, Context.MODE_MULTI_PROCESS);
    }
    gcm = GoogleCloudMessaging.getInstance(getBaseContext());
    SharedPreferences savedValues = PreferenceManager.getDefaultSharedPreferences(this);
    if(savedValues.getBoolean(getString(R.string.first_launch), true)){
        register();
        SharedPreferences.Editor editor = savedValues.edit();
        editor.putBoolean(getString(R.string.first_launch), false);
        editor.commit();
    }
    // Let AndroidMobilePushApp know we have just initialized and there may be stored messages
    sendToApp(new Bundle(), this);
}

and then my register method,

private void register() {
    new AsyncTask<Object, Object, Object>(){
        protected Object doInBackground(final Object... params) {
            String token;
            try {
                token = gcm.register(getString(R.string.project_number));
                Log.i("registrationId", token);
            } 
            catch (IOException e) {
                Log.i("Registration Error", e.getMessage());
            }
            return true;
        }
    }.execute(null, null, null);
}
1
It would be good if you share your code to get Device ID. and Device name on which the Device Token is not generating. - Waqar Khan
Please can you add your gcm register code? - msevgi
Hope the above snippets helps...! - Sathish
I am assuming that you are able to go through the try-catch portion without throwing an exception. Check that R.string.project_number returns a valid value on your test device. - Koh

1 Answers

0
votes
  1. On which device you are testing ? If it is pre 4.0.4 then you must be logged in with some google account. See for reference : Why google Account login is required for GCM to work for devices below 4.0.4 OS?

  2. Also check if have google play services installed on the device.

  3. Check in android manifest if the app package is same as in broadcast receiver you have registered.