Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Firebase-cloud-messaging Intent-filter to use for a 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();

   }
}

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"/>

to make sure the BroadcastReceiver fires when I plug cable to Android device. It works fine.

Therefore the issue lies with the:

<action android:name="com.google.firebase.MESSAGING_EVENT" />

Is this intent-filter action not recognized by the BroadcastReceiver? Is there another intent-filter action for Firebase messaging the BroadcastReceiver uses?

like image 742
Jupiter Avatar asked May 31 '17 13:05

Jupiter


People also ask

What are the two types of messages in Firebase Cloud Messaging?

Using Firebase Cloud Messaging, we can send three types of messages, i.e., Notification Message, Data Message, and the message with both Notification & Data Payload.

How pass data from BroadcastReceiver to activity in Android?

Step 1. Open your project where you want to implement this. Step 2. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.

What is Senderid in FCM?

The Sender ID is used in the client side application to register to FCM from the device. Copy the Server Key. You must provide the Server Key in the Engagement server while configuring an application to send push messages.


1 Answers

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.

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.

like image 171
Bob Snyder Avatar answered Nov 10 '22 20:11

Bob Snyder