RegistrationID 받았을때 Activity에 통지하기

@Override protected void onRegistered(Context context, String registrationId) { Log.d(“vpb”, “onRegistered”); Log.d(“vpb”, “ok! your registration-id is “+registrationId); // 기다리고 있는 Activity 에 통지하기 Intent intent = new Intent(); intent.setAction(SplashActivity.ACTION_GCM_REGISTRATION); intent.putExtra(“registrationId”, registrationId); context.sendBroadcast(intent); } SplashActivity는 아래와 같다.

public class SplashActivity extends Activity {
	public static final String ACTION_GCM_REGISTRATION = "jp.co.ais.game.vpb2014.intent.GCM_REGISTRATION";
	// ↓↓ BROADCAST RECEIVER ↓↓
	private BroadcastReceiver broadcastReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash);
		// /////////////////////////////////////////////////////////////////
		// RegistrationID 가져오기
		// /////////////////////////////////////////////////////////////////
		broadcastReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (action != null) {
					Log.e("vpb", "action => " + action);
					if ( action.equals(ACTION_GCM_REGISTRATION) ) {
						// Registration ID 통지
						String regstrationId = intent.getExtras().getString("registrationId");
						Log.e("vpb", "가져왔습니다! " + regstrationId);
						// TODO RegistrationID 가져온후에 할일은 여기에서.
					}
				}
			}
		};
		registerReceiver(broadcastReceiver, new IntentFilter(ACTION_GCM_REGISTRATION));
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
//		final String registrationId = GCMRegistrar.getRegistrationId(getApplicationContext());
//		if (registrationId.equals("")) {
//			Log.d("vpb", "request new registration-id");
//			GCMRegistrar.register(this, PushInfo.PROJECT_ID);
//		} else {
//			Log.d("vpb", "you already has a registration-id (" + registrationId + ")");
//		}
		// 매번 가지러 가기
		GCMRegistrar.register(this, PushInfo.PROJECT_ID);
	}
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(broadcastReceiver);
	}
	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(broadcastReceiver, new IntentFilter(ACTION_GCM_REGISTRATION));
	}
]]>

Related Posts