1

I have an app that can receive messages through Google Cloud Messaging. I need to automatically start an activity when a cloud message is received, waking up the device if necessary.

Though it may seem as a bad UI design where something is forcefully shown to user, this app is crucial to the job of the user, so it is much more useful to the user to be able to view this clearly, when a cloud message is arrived.

I tried calling startActivity() when a cloud message got received, and it works when the app is in the background. But, as soon as the app is removed from the recent app list, the new activity won't start.

Is there any way to achieve what I am trying to do?

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • You could just show a toast message that would clearly show up to the user. – Taylor Courtney Aug 17 '16 at 00:48
  • No way for toast to work when the app is in the background the i believe you might be able to do it with crouton. – Taylor Courtney Aug 17 '16 at 00:49
  • Typically you would show a heads up notification, which would launch an activity. That way it is also shown on the lock screen... – Daniel Nugent Aug 17 '16 at 01:18
  • First, to wake up the device if necessary, you can set the [priority to HIGH](https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message). Because it allows the GCM service to wake a sleeping device and open a network connection to your server. And for the activity to start even though you removed it in the app list, this SO question [24313539](http://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closed) and [22252065](http://stackoverflow.com/questions/22252065/refreshing-activity-on-receiving-gcm-push-notification) might help you – KENdi Aug 18 '16 at 09:55
  • When app is background, your intent service will not fired after notification. Notification will arrived in notification system try. for this reason your activity is not starting.you can see this answer. This is for fcm but gcm and fcm same things http://stackoverflow.com/a/39161526/3073945 but have you solved this issue? i am also searching this result. – Md. Sajedul Karim Nov 09 '16 at 19:03

1 Answers1

0

I assume you are receiving the messages using an IntentService.

If this is the case, you can launch an Activity using a flag:

Intent intent = new Intent (this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • This is exactly what I am currently doing. If the app is closed and removed from recent app list, activity won't be started. – Lahiru Chandima Aug 17 '16 at 01:53
  • @LahiruChandima Are you sure that the app itself isn't killed when you close and remove it from the recent app list? Reference [here](http://stackoverflow.com/a/37429495/4625829). – AL. Aug 20 '16 at 03:07