如何从推送通知启动后台运行的Android应用程序?

时间:2019-02-13 06:32:08

标签: android android-activity push-notification

我正在开发基于推送通知的android应用。当应用程序收到推送通知时,我需要打开(启动)Android应用程序。请解释。

我尝试了以下代码

      <activity                                                                                                                                                                                                           
      android:name="com.yamuko.driver.PickupRequestCustomDialogActivity"
      android:launchMode="singleTask"
      android:theme="@style/NoTitleDialog" />
在代码下面使用的onMessageReceived()中的

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

1 个答案:

答案 0 :(得分:1)

在收到推送通知(例如FCM)时,您不应启动“活动”。设备休眠且屏幕关闭时可能会出现这种情况。 相反,您应该创建一个通知。当用户打开设备并单击通知时,您的活动将启动。