Take the 2-minute tour ×

I have a working server/client solution that sends push notification to my device. The next step for my project would be to show a dialog on the activity window when the onReceive event is called in the C2DMReceiver class.

As I am new to android, I have no idea how to do this, so I would be really happy if someone could explain this to me.

Basically I reused the classes from the chrometophone Application for c2dm. The onReceive event is called, as I create a log entry for logcat. As the C2DMReceiver is a service, how to I get informed on my activity if there is a new message?

I googled a lot but couldn't find a working solution... I tried to use the registerReceiver() but I'm pretty sure I did it wrong. Does anyone have an example?

Ok, so here is what I got so far:


    BroadcastReceiver mReceiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
        Log.w(Consts.LOG_TAG_SERVICE, "Test");

    // Called when the activity is first created.
    public void onCreate(Bundle savedInstanceState) {

        Log.w(Consts.LOG_TAG_SERVICE, "started");


    // Called when the activity is restarted
    protected void onResume() {

        IntentFilter filter = new IntentFilter();

        registerReceiver(mReceiver, filter);

    // Called when the activity is closed
    protected void onPause() {


C2DMReceiver public class C2DMReceiver extends C2DMBaseReceiver {

public C2DMReceiver() {

    // TODO Load dynamic Gmail address

public void onRegistrered(Context context, String registrationId) {
    Log.i(Consts.LOG_TAG_SERVICE, registrationId);

    // Store the registration id in the preferences
    SharedPreferences settings = Prefs.get(context);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("deviceRegistrationID", registrationId);

    // TODO: Send ID to server

public void onUnregistered(Context context) {
    Log.w(Consts.LOG_TAG_SERVICE, "got here!");

public void onError(Context context, String errorId) {
    Log.w(Consts.LOG_TAG_SERVICE, errorId);

protected void onMessage(Context context, Intent intent) {
    Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));

    Intent broadcastIntent = new Intent();
    broadcastIntent.putExtra("reading", intent.getStringExtra("payload"));



This is all I got, but I never receive my own broadcast.. Does anyone have some inputs?

share|improve this question
Just for clarification, the line Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));is reached, right? –  uvesten Apr 14 '11 at 13:19
yes it is! I can see what I pushed to the device with logcat –  nino Apr 14 '11 at 13:49
Did my answer help you at all? –  uvesten Apr 27 '11 at 0:15

1 Answer 1

up vote 3 down vote accepted

This should do it.

protected void onMessage(Context context, Intent intent) {


  Intent i = new Intent(context, YourMainActivity.class);
  i.putExtra("reading", intent.getStringExtra("payload"));


You then handle the intent in your onStart in your main activity. If your activity is already running, it will be handled by the existing instance, if not, a new instance will be started.

share|improve this answer
Or, you could follow this guide to make a status bar notification, so that the user can select whether to start your app. Might be a cleaner way to do it: developer.android.com/guide/topics/ui/notifiers/… –  uvesten Apr 14 '11 at 15:10

Your Answer


