簡體   English   中英

在 Firebase 上打開活動收到通知

[英]open activity on Firebase notification received

我想在收到通知時自動打開一個活動,當應用程序在前台時它工作正常但當應用程序在后台時它不工作。

Intent intent = new Intent(this, NewPickUpRequestActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.putExtra(EXTRA_TRIP_ID, tripId);
            Helper.setStringValue(KEY_LAST_TRIP_TRIP_ID, tripId);
            startActivity(intent);

試試這個

Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.your.package", "com.your.package.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

當您無法啟動活動https://developer.android.com/guide/components/activities/background-starts時,您應該考慮一些例外情況

有兩種類型的消息數據消息和通知消息。 無論應用程序是在前台還是后台,數據消息都在 onMessageReceived 中處理。 數據消息是 GCM 傳統上使用的類型。 當應用程序在前台時,僅在 onMessageReceived 中接收通知消息。 當應用程序在后台時,會顯示自動生成的通知。 當用戶點擊通知時,他們將返回到應用程序。 包含通知和數據有效負載的消息被視為通知消息。 Firebase 控制台始終發送通知消息。

示例代碼:- 您需要像這樣在通知負載中指定 click_action

$noti = array
    (
    'icon' => 'new',
    'title' => 'title',
    'body' => 'new msg',
    'click_action' => 'open_NewPickUpRequestActivity'
);

現在在manifest文件中,在您的NewPickUpRequestActivity活動標簽中執行此操作

<activity
           android:name=".NewPickUpRequestActivity">
            <intent-filter>
                <action android:name="open_NewPickUpRequestActivity" /> // should be same as in click action
               <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
    </activity>

暫無
暫無

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

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