2

Can I automatically open an activity(MyActivity) of my app(MyApp) when I receive a push notification (without clicking on the notification)? I am thinking of using firebase cloud messaging (fcm) for push notification. I have done some digging around to find a solution and from some solutions, in StackOverflow it seems possible; below are the links I found:

https://stackoverflow.com/a/50368901/8444856

https://stackoverflow.com/a/30090042/8444856

https://stackoverflow.com/a/37626817/8444856

Open the app automatically when you receive a push notification with onesignal

If it's possible then is it also possible to open MyActivity from any of the below statuses:

  1. If the app is not open(hasn't been launched by the user previously)
  2. If the app is in the background and another app is running in the foreground
Community
  • 1
  • 1
Rashedul Hasan
  • 193
  • 3
  • 13

1 Answers1

1

Yeah ,It is possible.

public class NotificationReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    //Judging whether the app process survives
    if(SystemUtils.isAppAlive(context, "com.liangzili.notificationlaunch")){
        //If you survive, start DetailActivity directly, but consider the case that the app process is still there
        //But the Task stack is empty, such as when the user clicks the Back key to exit the application, but the process has not been recycled by the system, if started directly.
        //Detail Activity, press the Back key again and you won't return to MainActivity. So it's starting.
        //Before Detail Activity, start MainActivity.
        Log.i("NotificationReceiver", "the app process is alive");
        Intent mainIntent = new Intent(context, MainActivity.class);
        //Set the launch mode of mainativity to singletask, or add intent. flag stack clear_top to the following flag. If there are instances of mainativity in the task stack, it will be moved to the top, and the activities above it will be cleaned up. If there are no instances of mainativity in the task stack, it will be created at the top.
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Intent detailIntent = new Intent(context, DetailActivity.class);
        detailIntent.putExtra("name", "Rice cooker");
        detailIntent.putExtra("price", "58  dollars");
        detailIntent.putExtra("detail", "This is a good pot. This is where the app process exists. Start Activity directly.");

        Intent[] intents = {mainIntent, detailIntent};

        context.startActivities(intents);
    }else {
        //If the app process has been killed, restart app first, pass the start parameters of DetailActivity into Intent, and the parameters are passed into MainActivity through SplashActivity. At this time, the initialization of APP has been completed, and in MainActivity, you can jump to DetailActivity according to the input parameters.
        Log.i("NotificationReceiver", "the app process is dead");
        Intent launchIntent = context.getPackageManager().
                getLaunchIntentForPackage("com.liangzili.notificationlaunch");
        launchIntent.setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        Bundle args = new Bundle();
        args.putString("name", "Rice cooker");
        args.putString("price", "58 dollars");
        args.putString("detail", "This is a good pot. This is where the app process exists. Start Activity directly.");
        launchIntent.putExtra(Constants.EXTRA_BUNDLE, args);
        context.startActivity(launchIntent);
    }
}
}
  • I cannot find the class SystemUtils , where is it? – Mehmet Katircioglu May 23 '20 at 18:36
  • ...what? Are you checking if the process that your code is in is running? That makes no sense: if your code is running, your process is alive. Perhaps it's checking something more reasonable, but without the code for `SystemUtils.isAppAlive`, this answer is useless. – Ryan M May 10 '23 at 23:55