Skip to content

Commit 1e83580

Browse files
authored
Specify notification's dismiss intent target via action instead of co… (#5410)
…mponent name. * Changed createMessagingPendingIntent() to create an Intent targeting the BroadcastReceiver via action and package instead of specifying the ComponentName directly. This will make it easier to change FirebaseInstanceIdReceiver's class name in the future.
1 parent 4273a23 commit 1e83580

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

firebase-messaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Unreleased
22
* [changed] Added metadata to FirebaseInstanceIdReceiver to signal that it
33
finishes background broadcasts after the message has been handled.
4+
5+
* [changed] Specified notification's dismiss intent target via action instead of
6+
component name.
47

58
* [changed] Added Kotlin extensions (KTX) APIs from `com.google.firebase:firebase-messaging-ktx`
69
to `com.google.firebase:firebase-messaging` under the `com.google.firebase.messaging` package.

firebase-messaging/src/main/java/com/google/firebase/messaging/CommonNotificationBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.app.NotificationChannel;
2121
import android.app.NotificationManager;
2222
import android.app.PendingIntent;
23-
import android.content.ComponentName;
2423
import android.content.ContentResolver;
2524
import android.content.Context;
2625
import android.content.Intent;
@@ -71,7 +70,7 @@ public final class CommonNotificationBuilder {
7170
// resource file exists (possibly stripped by Progurad). Note this name is set purposefully
7271
// different from FM's default resource channel name "Miscellaneous" for debugability.
7372
private static final String FCM_FALLBACK_NOTIFICATION_CHANNEL_NAME_NO_RESOURCE = "Misc";
74-
private static final String ACTION_MESSAGING_EVENT = "com.google.firebase.MESSAGING_EVENT";
73+
private static final String ACTION_RECEIVER = "com.google.android.c2dm.intent.RECEIVE";
7574

7675
// Getting illegal resouce id from context will throw NotFoundException.
7776
// See: https://developer.android.com/reference/android/content/res/Resources#ID_NULL
@@ -559,15 +558,14 @@ private static PendingIntent createDeleteIntent(
559558
return createMessagingPendingIntent(callingContext, appContext, dismissIntent);
560559
}
561560

562-
/** Create a PendingIntent to start the app's messaging service via FirebaseInstanceIdReceiver */
561+
/** Create a PendingIntent for the app's messaging receiver. */
563562
private static PendingIntent createMessagingPendingIntent(
564563
Context callingContext, Context appContext, Intent intent) {
565564
return PendingIntent.getBroadcast(
566565
callingContext,
567566
generatePendingIntentRequestCode(),
568-
new Intent(ACTION_MESSAGING_EVENT)
569-
.setComponent(
570-
new ComponentName(appContext, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
567+
new Intent(ACTION_RECEIVER)
568+
.setPackage(appContext.getPackageName())
571569
.putExtra(IntentKeys.WRAPPED_INTENT, intent),
572570
getPendingIntentFlags(PendingIntent.FLAG_ONE_SHOT));
573571
}

firebase-messaging/src/test/java/com/google/firebase/messaging/CommonNotificationBuilderRoboTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
package com.google.firebase.messaging;
1515

1616
import static com.google.common.truth.Truth.assertThat;
17-
import static com.google.firebase.messaging.ServiceStarter.ACTION_MESSAGING_EVENT;
1817
import static org.robolectric.Shadows.shadowOf;
1918

2019
import android.app.Notification;
21-
import android.content.ComponentName;
2220
import android.content.Context;
2321
import android.content.Intent;
2422
import android.content.pm.ApplicationInfo;
@@ -66,6 +64,7 @@ public class CommonNotificationBuilderRoboTest {
6664
private static final String NOTIFICATION_PRIORITY_DEFAULT = "0";
6765
private static final String NOTIFICATION_PRIORITY_NEGATIVE = "-999";
6866
private static final int DEFAULTS_ALL_OFF = 0;
67+
private static final String ACTION_RECEIVER = "com.google.android.c2dm.intent.RECEIVE";
6968

7069
private Context appContext;
7170
private Context callingContext;
@@ -802,10 +801,8 @@ public void createNotificationInfo_deleteIntentWithAnalytics() {
802801
assertThat(deletePendingIntent.isBroadcastIntent()).isTrue();
803802
assertThat(deletePendingIntent.getSavedContext()).isEqualTo(callingContext);
804803
Intent deleteIntent = deletePendingIntent.getSavedIntent();
805-
assertThat(deleteIntent.getComponent())
806-
.isEqualTo(
807-
new ComponentName(appContext, "com.google.firebase.iid.FirebaseInstanceIdReceiver"));
808-
assertThat(deleteIntent.getAction()).isEqualTo(ACTION_MESSAGING_EVENT);
804+
assertThat(deleteIntent.getPackage()).isEqualTo(appContext.getPackageName());
805+
assertThat(deleteIntent.getAction()).isEqualTo(ACTION_RECEIVER);
809806
Intent dismissIntent = deleteIntent.getParcelableExtra(IntentKeys.WRAPPED_INTENT);
810807
assertThat(dismissIntent).isNotNull();
811808
assertThat(dismissIntent.getAction()).isEqualTo(IntentActionKeys.NOTIFICATION_DISMISS);

firebase-messaging/src/test/java/com/google/firebase/messaging/FirebaseMessagingServiceRoboTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import android.app.NotificationManager;
4444
import android.app.PendingIntent;
4545
import android.content.BroadcastReceiver;
46-
import android.content.ComponentName;
4746
import android.content.Context;
4847
import android.content.Intent;
4948
import android.content.IntentFilter;
@@ -90,6 +89,7 @@ public class FirebaseMessagingServiceRoboTest {
9089

9190
// Constants are copied so that tests will break if these change
9291
private static final String ACTION_NEW_TOKEN = "com.google.firebase.messaging.NEW_TOKEN";
92+
private static final String ACTION_RECEIVER = "com.google.android.c2dm.intent.RECEIVE";
9393

9494
private static final String DEFAULT_FROM = "123";
9595
private static final String DEFAULT_TO = "456";
@@ -585,8 +585,8 @@ private void dispatchBroadcastIntentToReceiver(PendingIntent pi) throws Interrup
585585
// Ensure the intent is a broadcast to the receiver, then dispatch it
586586
assertWithMessage("expected broadcast intent").that(shadowOf(pi).isBroadcastIntent()).isTrue();
587587

588-
assertEquals(
589-
intent.getComponent(), new ComponentName(context, FirebaseInstanceIdReceiver.class));
588+
assertEquals(intent.getAction(), ACTION_RECEIVER);
589+
assertEquals(intent.getPackage(), context.getPackageName());
590590
sendBroadcastToReceiver(intent);
591591
ShadowLooper.idleMainLooper();
592592
}

0 commit comments

Comments
 (0)