7

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?

Jupiter
  • 615
  • 1
  • 6
  • 15

1 Answers1

8

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.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • So say I create an android service instead of BroadcastReceiver that I never destroy which listens for `com.google.firebase.MESSAGING_EVENT` . Could this work? – Jupiter May 31 '17 at 16:54
  • Yes. That is the usual way to receive messages, which is described in the [Setup documentation](https://firebase.google.com/docs/cloud-messaging/android/client#manifest). All you need to do is define a service that extends [FirebaseMessagingService](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService) and declare it in your manifest. – Bob Snyder May 31 '17 at 17:06
  • Indeed. I have done so from the beginning however this implementation does not solve my problem. `FirebaseMessagingService`class only fires when the app is launched. I am trying to do work in a service when the app is not launched by event of a Firebase message. What else could you suggest sir? – Jupiter May 31 '17 at 17:56
  • I can't explain the behavior you are reporting. Normally, receipt of a Firebase cloud message will always be delivered to the app's FirebaseMessagingService. If the app is not running, the system will create an instance of the app, start the service, and deliver the message. I was just testing that scenario today, so am confident of that behavior. I can only suggest you add debug output to your service lifecycle methods and do more testing. – Bob Snyder May 31 '17 at 18:11
  • 4
    @Jupiter: On some phones, it's possible to close an app in a way that inhibits it from receiving messages. This issue is discussed in the comments to this question: https://stackoverflow.com/q/44193726/4815718 – Bob Snyder May 31 '17 at 20:36
  • That is exactly the answer to my problem! Greatest appreciations @Bob – Jupiter May 31 '17 at 21:18
  • Here is a good explanation too: [link] https://stackoverflow.com/a/37845174/5620779 – Jupiter May 31 '17 at 21:19