亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Android 應用程序處于后臺時從 firebase-message 獲取數據

如何在 Android 應用程序處于后臺時從 firebase-message 獲取數據

慕神8447489 2023-03-31 15:03:00
我正在使用 firebase 控制臺發送 firebase 消息。這些消息應包含如下所示的附加數據,目的是在我的應用程序的 webview 中打開特定的 URL:我設置了我的清單和 firebase 類來獲取消息。在我的 firebase 類中,我嘗試獲取數據:@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) {    super.onMessageReceived(remoteMessage);    Intent intent = new Intent(this, MainActivity.class);    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    if(remoteMessage.getData().containsKey("key1")) {        intent.putExtra("destination", remoteMessage.getData().get("key1"));    }    PendingIntent pendingIntent = PendingIntent.getActivity    (this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);    String channelId = "default";    NotificationCompat.Builder builder;    if (remoteMessage.getNotification() != null ) {        if (remoteMessage.getNotification().getTitle() != null) {            builder = new NotificationCompat.Builder(this, channelId)                    .setSmallIcon(R.drawable.ic_stat_onesignal_default)                    .setContentTitle(remoteMessage.getNotification().getTitle())                    .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))                    .setAutoCancel(true)                    .setContentIntent(pendingIntent);        } else {            builder = new NotificationCompat.Builder(this, channelId)                    .setSmallIcon(R.drawable.ic_stat_onesignal_default)                    .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))                    .setAutoCancel(true)                    .setContentIntent(pendingIntent);        }但如果應用程序從后臺啟動,則事件不會觸發。我試圖獲取意圖并檢查活動的 onResume() 事件中的密鑰,但它不起作用(在 inCreate() 和 onStart() 中相同)。有誰能夠幫助我?
查看完整描述

4 回答

?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

推送消息有3種類型

  • 通知

  • 數據

  • 和兩者

推送消息基本上是一個 json 負載:

payload:{

    notificacion...

    data

}

每種類型的推送消息的規則是不同的。在您的情況下,您正在使用 Firebase Web 控制臺并添加自定義數據,這意味著您的有效負載將包含通知和數據。


對于組合類型,背景中的行為是使用默認通知(NotificationCompat,視覺類型)并打開清單中注冊的默認活動。在活動中,您可以獲得數據。


假設您的默認活動稱為 MainActivity


public class MainActivity {


    onCreate...{

    //... usual stuff

    Intent fcmIntent = getIntent();

    if fcmIntent != null

    //check the extras and forward them to the next activity if needed

    }

}


查看完整回答
反對 回復 2023-03-31
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

有兩種類型的推送消息

(1)Notification Message(應用在前臺時會收到)

(2)Data Message(app在后臺+前臺時會收到)

http://img3.sycdn.imooc.com/642685f700012c0c06600116.jpg


查看完整回答
反對 回復 2023-03-31
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

您需要在 firebase 通知數據集中設置 click_action 才能從后臺接收數據并實現 onMessageReceived 來處理前臺數據


查看完整回答
反對 回復 2023-03-31
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

我解決了這個購買處理通知,handleIntent當應用程序被殺死時,在后臺和前臺。我完全忽略了該onMessageReceived(@NonNull RemoteMessage message)方法,因為當應用程序處于后臺時它似乎不起作用。


@Override


    public void handleIntent(Intent intent) {

        Log.d( "FCM", "handleIntent \n"+intent.getStringExtra("data"));

        String messageTitle = intent.getStringExtra("title");

        String messageBody = intent.getStringExtra("body");

        //from here pass the values to a method to show your notification.

        

    }

在您的活動中,您將能夠從意圖中檢索任何數據。


要重定向到您想要的活動,您必須將 click_action 添加到您的負載中。這是我的有效負載的樣子:


"notification": {

  "body": "Your message is here",

  "title": "Hey Robby",

  "discount_code": "baba-blacksheep",

  "uid": "2",

  "click_action": "OPEN_ACTIVITY_FROM_NOTIFICATION"

}


查看完整回答
反對 回復 2023-03-31
  • 4 回答
  • 0 關注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號