0

我正在編程一個應用程序,它從FCM消息接收一些數據,然後根據消息中包含的數據有效載荷更新UI。從主線程中的Firebase服務訪問數據

該服務運行正常,並接收數據,但我不知道如何讓它回到我的主要活動。

的火力地堡服務在我的表現爲

<service android:name=".MyFirebaseService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
    </intent-filter> 
</service> 

在我MyFirebaseService類我已經覆蓋了onMessageReceived方法聲明,但我怎麼來通知收到郵件我的主要活動?

有沒有可以與Firebase服務一起使用的處理程序?

+0

燁,你可以聽的意圖來主要活動 –

+0

我不明白你需要什麼? –

回答

1
dev.android.com > Develop > Training > Background Jobs > Reporting Work Status 

https://developer.android.com/training/run-background-service/report-status.html

聽要發送的狀態IntentService中的工作請求首先創建一個Intent,其中包含 擴展數據中的狀態。

接下來,通過調用 LocalBroadcastManager.sendBroadcast()發送意圖。這會將Intent發送到已註冊接收它的應用程序中的任何 組件。

Intent localIntent = new Intent(BROADCAST_ACTION).putExtra(DATA_STATUS, status); 
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); 

接收廣播意向對象,請使用 廣播接收器的子類。在子類中,實現 BroadcastReceiver.onReceive()回調方法,LocalBroadcastManager在接收到Intent時調用。 LocalBroadcastManager將傳入的意圖傳遞給 BroadcastReceiver.onReceive()

爲了與 系統中註冊的BroadcastReceiver和IntentFilter的,得到LocalBroadcastManager的實例,並調用它的 registerReceiver()方法。

LocalBroadcastManager.getInstance(this) 
    .registerReceiver(mDownloadStateReceiver, statusIntentFilter); 
1

這裏是我的消息處理

Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("title", fcmTitle); 
    intent.putExtra("summary", fcmSummary); 
    intent.putExtra("message", fcmMessage); 
    intent.putExtra("imageUrl", fcmImageUrl); 
    intent.putExtra("goto", fcmGoto); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(App.getAppContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT); 

,你可以有這樣的事情在主要活動

Boolean bolGoto = false; 
    if (getIntent().getExtras() != null) { 
     for (String key : getIntent().getExtras().keySet()) { 
      //String value = getIntent().getExtras().getString(key); 
      if (key.equals("goto")){ 
       bolGoto = true; 
      } 
     } 
    } 
    if (bolGoto){ 
     handleFCMNotification(getIntent()); 
    }