【问题标题】:Is it possible to automatically open an app's activity from push notification in andorid- without clicking on the notification是否可以从android中的推送通知中自动打开应用程序的活动-无需单击通知
【发布时间】:2019-02-28 09:48:47
【问题描述】:

当我收到推送通知(不点击通知)时,我可以自动打开我的应用(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. 如果应用在后台,而另一个应用在前台运行

【问题讨论】:

  • 显示您创建通知的代码
  • 是的,你可以......你可以在未决意图的情况下做到这一点
  • 是的,您也可以使用广播接收器来实现。
  • 你可以在这里使用深度链接概念

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


【解决方案1】:

是的,有可能。

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

【讨论】:

  • 我找不到 SystemUtils 类,它在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多