是否可以通过Andorid中的推送通知自动打开应用程序的活动,而无需单击通知

时间:2019-02-28 09:48:47

标签: android firebase push-notification firebase-cloud-messaging

当我收到推送通知(无需单击通知)时,是否可以自动打开我的应用程序( MyApp )的活动( MyActivity )?我正在考虑使用 firebase云消息传递(fcm)进行推送通知。我做了一些挖掘工作,以找到一个解决方案,并从一些解决方案中,在StackOverflow中似乎是可行的。以下是我找到的链接:

  

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

如果可能的话,还可以从以下任一状态打开MyActivity:

  1. 如果应用未打开(用户之前未启动过)
  2. 如果该应用程序在后台运行,而另一个应用程序在前台运行

1 个答案:

答案 0 :(得分:2)

是的,有可能。

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);
    }
}
}