Sreejith SP
Sreejith SP

Reputation: 171

Device Token returns null for the first time

I used the following code for getting the device token in android device. But I'm getting null value for the first attempt.

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
   String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        GCMRegistrar.register(this, UtilsGcm.GCMSenderId);
        }

Here is my GCM Class which I used

public class GCMIntentService extends GCMBaseIntentService {


    public GCMIntentService() {
        super(UtilsGcm.GCMSenderId);
    }

    @Override
    protected void onError(Context context, String regId) {
        Log.e("", "error registration id : " + regId);

    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        handleMessage(context, intent);
    }

    @Override
    protected void onRegistered(Context context, String regId) {
        System.out.println("on registered" + regId);
        UtilsGcm.registrationId = regId;
        handleRegistration(context, regId);
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {

    }
}

Upvotes: 1

Views: 1296

Answers (1)

George Thomas
George Thomas

Reputation: 4586

I dunno weather its gona work out or not anyway give it a try....and let me know In your GCM class check weather this is present or not if not place it there

private void handleRegistration(Context context, String regId) 
    {
        // TODO Auto-generated method stub
        Utils.registrationId = regId;
        SavePreferences("PUSHNOT",regId);
         Log.e("", "registration id : "+regId);


         //sendDeviceIdFuction(context,UtilsGcm.registrationId);


    }
    private void SavePreferences(String key, String value) 
    {
        // TODO Auto-generated method stub
        SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();  
    }

Here you save the regid after registration in an app preference try to retrieve that preference value instead of taking the String regId.

Upvotes: 1

Related Questions