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()); 
    }