6

Here is the code that is used as BroadcastReceiver inside my MainActivity

    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/

Renz Manacmol
  • 869
  • 12
  • 27
  • I think closing the app by sliding out is force closing the app so it will not received a GCM message unless user start the app again. Please see my answer in the previous [SO question](http://stackoverflow.com/questions/37322566/how-whatsapp-manage-to-restart-its-services-and-keep-receiving-gcm-after-being-f) – Android Enthusiast May 22 '16 at 06:42
  • 1
    I did it by using WakefulBroadcastReceiver – Renz Manacmol May 22 '16 at 06:47
  • 1
    You can receive GCM (now FCM) messages whether your application is open or closed. I would advise against writing your own WakefulBroadcastReceiver, please use the provided GcmListenerService or the even easier to use FirebaseMessagingService. See sample: https://github.com/firebase/quickstart-android/tree/master/messaging – Arthur Thompson May 23 '16 at 16:15
  • @acer how did you solve it? – natsumiyu Nov 07 '16 at 07:40

2 Answers2

14

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:

..It won't directly causes services to stop..

Since listeners for GCM notifications are Services, 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:

For stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.

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:

Apps that are in the stopped state do not receive broadcast Intents.

Stopped state is:

when the app is initially installed (before the user runs something in the app) or after a Force Stop. You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols

Hope this helps clear some things somehow. Cheers! :D

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
1

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.

Dhaval
  • 2,724
  • 2
  • 24
  • 33
  • There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here). – Cecil Paul Dec 17 '19 at 06:55
  • There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible. This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over. – Cecil Paul Dec 17 '19 at 06:55