Here is the code that is used as BroadcastReceiver inside my MainActivity
//Initializing our broadcast receiver
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();
}
}
};
I followed this tutorial about GCM
https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/
You can try with
GcmListenerService
When you close your app by sliding out the service is still activated in background and you can receive push notification. Unless user manually force stop app from setting, this service helps you.Closing the app by sliding it (more commonly known as swiping it away) doesn't totally kill the app. Check out this answer in Android Enthusiasts community for a more detailed description. You can see there that it is mentioned that:
Since listeners for GCM notifications are
Service
s, this would actually mean that there is still a possibility that your app may still continue to receive them regardless if it is swiped away.Though the result of swiping an app may also differ depending on the device it is running, one may kill/force stop it or other just as mentioned above, will stop background processes.
If the result is, however, the app is killed/force stopped, as mentioned in the answer from the link above:
Which would result for the app to don't receive any kind of notifications at all, as it was designed for Android since version 3.1, as stated in this answer:
Hope this helps clear some things somehow. Cheers! :D