I have a an application targeting API >= 22 with a unique, single-top activity configured this way in the manifest:
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/Theme.MyApplication.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Notice the android.intent.action.MAIN
string above.
I have a cloud (Firebase) messaging service configured this way:
<service
android:name=".MyService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
My typical usage scenario is when my application already is running in the foreground when receiving messages.
When an incoming message is received, my service's onMessageReceived
method is run, and does the following:
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
[...]
Intent intent = new Intent("com.example.myapplication.myservice");
intent.putExtra("foo", "bar");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode++, intent, PendingIntent.FLAG_ONE_SHOT);
showNotification(
getTitle(),
getBody(),
pendingIntent);
}
Notice the "com.example.myapplication.myservice"
action string above.
I have a fragment monitoring broadcast messages this way:
IntentFilter filter = new IntentFilter("com.example.myapplication.myservice");
requireActivity().registerReceiver(new MyBroadcastReceiver(), filter);
The receiver does get called successfully, receiving the message created when onMessageReceived
was called.
Is this normal, knowning that the <intent-filter>
tag for the activity in my manifest should only let android.intent.action.MAIN
intent actions in?
Yes, this is normal. You are confusing 2 things.
The <intent-filter>
declaration in the manifest controls what Intent
actions are used to launch the Activity
.
This has nothing to do with broadcast Intent
s. Broadcast Intent
s are used to trigger BroadcastReceiver
s and these have nothing to do with activities.