MainActivity 内で BroadcastReceiver として使用されるコードは次のとおりです。
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
//When the broadcast received
//We are sending the broadcast from GCMRegistrationIntentService
@Override
public void onReceive(Context context, Intent intent) {
//If the broadcast has received with success
//that means device is registered successfully
if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
//Getting the registration token from the intent
String token = intent.getStringExtra("token");
//Displaying the token as toast
Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();
//if the intent is not with success then displaying error messages
} else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
}
}
};
GCMに関するこのチュートリアルに従いました
https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/