簡體   English   中英

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

[英]How to get data from firebase-message while Android app is in background

我正在使用 firebase 控制台發送 firebase 消息。 消息應包含如下所示的附加數據,目的是在我的應用程序中的 webview 中打開特定的 URL:

在此處輸入圖像描述

我設置了我的清單和 firebase class 來獲取消息。 在我的 firebase class 中,我嘗試獲取數據:

@Override
public 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);
        }

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "default", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }

        manager.notify(0, builder.build());
    }
}

在我的 MainActivity class 中,我嘗試獲取數據。 當應用程序在前台時,以下工作(無論打開什么活動,它都會跳轉到 MainActivity 並執行以下內容):

@Override
protected void onNewIntent(Intent intent) {
    if (intent.getExtras() != null) {

        Bundle extras = intent.getExtras();

        if(extras.containsKey("destination")) {
            Log.e("FIREBASE_CONTAINS", (String) extras.get("destination"));
        }
    }
}

但是如果應用程序從后台啟動,則不會觸發該事件。 我試圖獲取意圖並檢查活動的 onResume() 事件中的密鑰,但它不起作用(在 inCreate() 和 onStart() 中相同)。

有誰能夠幫我?

- - - - - - 編輯 - - - - - - - - -

如評論之一所述,問題似乎是 Notification-Messages 不會到達 onMessageReceived() 事件。 顯然 firebase 控制台無法發送數據通知(這將到達事件)所以我嘗試使用 POSTMAN。 我已經讀到我必須將通知標簽從消息正文中刪除,並將我的所有信息放在數據部分中。 但是如果我這樣做,消息將不會到達我的應用程序(當我再次添加通知部分時它們會到達,但在這種情況下它們當然不會到達 onMessageReceived() 事件)。

推送消息有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
    }
}

推送消息有兩種類型

(1)通知消息(當應用程序處於前台時會收到)

(2)數據消息(當應用程序在后台+前景時將收到)

在此處輸入圖片說明

參考: https : //firebase.google.com/docs/cloud-messaging/android/receive

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

在此處查看更新的答案: https://stackoverflow.com/a/73724040/7904082

暫無
暫無

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

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