簡體   English   中英

如何從推送通知啟動后台運行 Android 應用程序?

[英]how to launch background running android app from 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);

收到推送通知(例如 FCM)時,您不應啟動 Activity。 因為它可能會在設備處於睡眠狀態且屏幕關閉時出現。 相反,您應該創建一個通知。 當用戶打開設備並單擊通知時,您的活動就會啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM