Search code examples
androidandroid-c2dm

Can I register for C2DM (push notification) in my splash screen?


Is it alright to register for C2DM in my splash screen?

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class myMain extends Activity {
/** Called when the activity is first created. */



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

    MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.musicsplash);
    mpSplash.start();

    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));

    registrationIntent.putExtra("sender", "my email address");

    startService(registrationIntent);

    Thread logoTimer = new Thread(){
        public void run(){
            try{
                sleep(4000);
                startActivity(new Intent("com.ishop.pizzaoven.CLEARSCREEN"));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              finally{
                finish();
            }
        }
    };
    logoTimer.start(); 
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}

}


Solution

  • Yes. You can register for C2DM wherever you like, and often the sooner the better, so the app is ready to receive the messages. Note you do not need to re-register EVERY time the app runs, register once, store it to prefs, and only register again if the pref is empty (e.g. after an uninstall/reinstall)