Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:35:16+00:00 2026-06-15T08:35:16+00:00

I am embedding GCM for push notification in my app. I’m facing a very

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:35:17+00:00Added an answer on June 15, 2026 at 8:35 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am embedding my videos with JW player http://www.longtailvideo.com/players/ and I have a problem
I am embedding a report into my ASP.NET app using the report viewer control.
I am embedding a 3rd party app into a panel on a C# Windows
Having a problem embedding a flash chatroom under a doctype equaling strict using the
I'm embedding here a simplified version of a code that is not working for
I'm embedding a Flash ActiveX control in my C++ app (Flash.ocx, Flash10a.ocx, etc depending
The problem: I am embedding a CSS file into a custom control library with
I’m embedding YouTube videos in a UIWebView for an iOS app. I’m using Method
When embedding a JavaScript interpreter (Rhino) into a Java application (to be able to
While embedding a SWF in HTML is not too hard, there are a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.