-2

I'm developing push notification base android app. I need to open (launch) android app when the application received a push notification. please explain.

i tried following code

      <activity                                                                                                                                                                                                           
      android:name="com.yamuko.driver.PickupRequestCustomDialogActivity"
      android:launchMode="singleTask"
      android:theme="@style/NoTitleDialog" />

in onMessageReceived() used below code.

            Intent home = new Intent();
            Bundle extras = new Bundle();
            home.putExtras(extras);

            home.setAction(Intent.ACTION_MAIN);
            home.addCategory(Intent.CATEGORY_LAUNCHER);
            home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            ComponentName cn = new ComponentName(getApplicationContext(), PickupRequestCustomDialogActivity.class);
            home.setComponent(cn);
            getApplication().startActivity(home);
Bunny
  • 1,044
  • 12
  • 23
  • 1
    Possible duplicate with https://stackoverflow.com/questions/13716723/open-application-after-clicking-on-notification – Bach Vu Feb 13 '19 at 06:34

1 Answers1

0

You should not launch Activity when receive push notification (e.g. FCM). As it may come when device is sleeping and screen is off. Instead, you should create a notification. When user turns on the device and click the notification, your Activity is launched.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
  • let's say the user has launched the youtube app while my app is running on the background. how can I give priority to my app and launch it? – Aruna D Mahagamage Feb 13 '19 at 07:32
  • You can't do it and you shouldn't do it. Even all instant messengers will not do it though they are "instant", they only display a notification. User is the owner of the device, you are not. – AIMIN PAN Feb 13 '19 at 07:39