1

I need to implement this feature but I am not sure if this is possible on android.

We have gcm listener service running. But let's say the app is open, and a notification arrives from the server. I need to have an activity triggered automatically without touching the notification status bar on the top of the phone screen.

In other words, without any user interaction once the notification arrives, if the app is running, an activity must be triggered immediately.

I took a look at this thread, but this is not really what I need.

Intent - if activity is running, bring it to front, else start a new one (from notification)

any clues or more info? thx!

Community
  • 1
  • 1
gmmo
  • 2,577
  • 3
  • 30
  • 56

1 Answers1

0

You can start an activity without another prior activity by using the FLAG_ACTIVITY_NEW_TASK flag.

Context c = getApplicationContext(); // or getContext(), or any other context you can find from the current app
Intent i = new Intent(c, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • this is not really what I want. My activity is already running. When the gcm (background service) creates a new notification, I need in somehow notify my running activity that it needs to update its content. Like google email. If you have the app open, and a new email arrives. Without touching anything you see a new email showing up. The background service is in somehow letting the running activity know it needs to update its content. Not sure if I made it clear. – gmmo Feb 11 '16 at 19:17
  • 1
    This is not what you stated in the original question. If you just want an activity to be able to "listen" to an event like a GCM, you could communicate between your activity and the broadcast receiver by using a LocalBroadcastManager or event bus. You will have to wire this up yourself. – Doug Stevenson Feb 11 '16 at 19:24