Android GCM: Not getting registration token on onCreate() method

androidandroid-notificationsgoogle-cloud-messaging

I am embedding GCM for push notification in my app. I'm facing a very weird problem, on first run I'm not able to get GCM registration token, but when you run my app second time you will get the registration ID printing on the console. I don't know what am I doing worng. Here is what I have done so far.

This is my onCreate() method where I want to print GCM regID:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String regId = GCMRegistrar.getRegistrationId(this);
    GCM_regID = regId;
            System.out.println("GCM regId: "+GCM_regID);

Doing the following code inside onCreate():

/**
     * Google Cloud Messaging - Getting server Url and device ID to send it
     * across Google server for the notification..
     */
    mGCMReceiver = new GCMReceiver();
    mOnRegisteredFilter = new IntentFilter();
    mOnRegisteredFilter.addAction(Constants.ACTION_ON_REGISTERED);

    if (Constants.SENDER_ID == null) {
        // mStatus.setText("Missing SENDER_ID");
        return;
    }
    if (Constants.SERVER_URL == null) {
        // mStatus.setText("Missing SERVER_URL");
        return;
    }

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    if (!regId.equals("")) {
        sendIdToServer(regId);
    } else {
        GCMRegistrar.register(getApplicationContext(), Constants.SENDER_ID);
    }

    sendIdToServer(regId);

}

Sending GCM_regId to server via these method as guided in one of the tutorial:

/**
 * GCM - sending the data in json format to server db
 * */
public void sendIdToServer(String regId) {
    (new SendRegistrationIdTask(regId)).execute();
    GCM_regID = regId;
}

private class GCMReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String regId = intent
                .getStringExtra(Constants.FIELD_REGISTRATION_ID);
        token = regId;

    }
}

private final class SendRegistrationIdTask extends
        AsyncTask<String, Void, HttpResponse> {
    // private String mRegId;

    public SendRegistrationIdTask(String regId) {
        // mRegId = regId;
    }

    @Override
    protected HttpResponse doInBackground(String... regIds) {
        // String url = Constants.SERVER_URL + "/register";

        return null;
    }

    @Override
    protected void onPostExecute(HttpResponse response) {
        if (response == null) {

            return;
        }

        StatusLine httpStatus = response.getStatusLine();
        if (httpStatus.getStatusCode() != 200) {
            Log.e(Constants.TAG, "Status: " + httpStatus.getStatusCode());
            return;
        }
    }
}

I don't think so, GCMIntentService class is needed here for my problem. Please look into this and help me in getting out of this issue.

I'm able to print in GCMIntentService class, on onRegistered(). Here it goes:

@Override
protected void onRegistered(Context context, String regId) {
    Intent intent = new Intent(Constants.ACTION_ON_REGISTERED);
    intent.putExtra(Constants.FIELD_REGISTRATION_ID, regId);
    context.sendBroadcast(intent);
}

I have to print the regId on MainActivity, on onCreate().

Best Answer

Registering the device will take some time.. So if you will try to retrieve the registration id immediately after registering the device in onCreate() then every time it will return a null value.. So try to register your device inside onCreate() and retrieve the id in any different activity/Service (You can retrieve the Id from GCMIntentService class the api from GCM). Note method of GCM Intent Service class.

    protected void onRegistered(Context arg0, String arg1) {
    Logger.d(arg0, "REG ID="+arg1);
    regID = arg1;
}

This is method is called after Reg is done. So for my case the regID i took this as a static string and am accessing it else. OR Like u want the regID on Activity B it is preferred to Register it on Activity A and retrieve it via the static string in the above method from the GCMIntent Class.

Related Topic