问题描述
我试图在Android系统收到Firebase Cloud消息通知时运行Android BroadcastReceiver。
I am trying to get the Android BroadcastReceiver to run when a Firebase Cloud message notification is received by the Android system.
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "EVENT OCCURED", Toast.LENGTH_LONG).show();
}
}
在AndroidManifest中需要指定接收方标签,例如:
It is required in the AndroidManifest to specify receiver tags as such:
<receiver
android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</receiver>
正如您在上面的清单中所见,我已经添加:
As you can see in the Manifest above I've added:
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
确保当我将电缆插入Android设备时可以触发BroadcastReceiver。
to make sure the BroadcastReceiver fires when I plug cable to Android device. It works fine.
因此,问题在于:
<action android:name="com.google.firebase.MESSAGING_EVENT" />
此Intent-filter操作是否未被BroadcastReceiver识别?
BroadcastReceiver使用的Firebase消息是否还有另一个意图过滤器动作?
Is this intent-filter action not recognized by the BroadcastReceiver?Is there another intent-filter action for Firebase messaging the BroadcastReceiver uses?
推荐答案
Android意图消息传递系统处理意图在三个渠道中:活动,服务和广播接收器。 的方法表示渠道类型: startActivity()
, startService()
和 sendBroadcast()
。用 startService()
发布的意图仅与服务的意图过滤器匹配。
The Android Intent messaging system processes Intents in three "channels": Activities, Services and BroadcastReceivers. The methods for publishing an Intent indicate the channel type: startActivity()
, startService()
and sendBroadcast()
. An intent published with startService()
will only match the intent filter of a Service. It is not tested against Activities and BroadcastReceivers.
因为操作 com.google.firebase.MESSAGING_EVENT
通常被接收,因此未针对活动和广播接收器进行测试。如果服务是从 FirebaseMessagingService
派生的,则必须在操作通道中发布操作。您将无法使用BroadcastReceiver接收它。
Because action com.google.firebase.MESSAGING_EVENT
is normally received by a Service derived from FirebaseMessagingService
, it must be the case that the action is published in the Service channel. You will not be able to receive it with a BroadcastReceiver.
这篇关于什么用于BroadcastReceiver的Firebase云消息传递Intent过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!