@@ -95,21 +95,25 @@ public final class CommonNotificationBuilder {
95
95
// Do not instantiate.
96
96
private CommonNotificationBuilder () {}
97
97
98
+ /** Creates a DisplayNotificationInfo from NotificationParams for a single Context. */
98
99
static DisplayNotificationInfo createNotificationInfo (
99
100
Context context , NotificationParams params ) {
100
101
Bundle manifestMetadata =
101
102
getManifestMetadata (context .getPackageManager (), context .getPackageName ());
102
103
103
104
return createNotificationInfo (
104
105
context ,
105
- context . getPackageName () ,
106
+ context ,
106
107
params ,
107
108
getOrCreateChannel (context , params .getNotificationChannelId (), manifestMetadata ),
108
- context .getResources (),
109
- context .getPackageManager (),
110
109
manifestMetadata );
111
110
}
112
111
112
+ /**
113
+ * Legacy method that creates a DisplayNotificationInfo from NotificationParams that allows
114
+ * specifying components so the calling Context can be different from the Context used for the
115
+ * notification (resources, etc.).
116
+ */
113
117
public static DisplayNotificationInfo createNotificationInfo (
114
118
Context context ,
115
119
String pkgName ,
@@ -118,8 +122,53 @@ public static DisplayNotificationInfo createNotificationInfo(
118
122
Resources appResources ,
119
123
PackageManager appPackageManager ,
120
124
Bundle manifestMetadata ) {
125
+ return createNotificationInfo (
126
+ context ,
127
+ context ,
128
+ params ,
129
+ channelId ,
130
+ manifestMetadata ,
131
+ pkgName ,
132
+ appResources ,
133
+ appPackageManager );
134
+ }
121
135
122
- NotificationCompat .Builder builder = new NotificationCompat .Builder (context , channelId );
136
+ /**
137
+ * Creates a DisplayNotificationInfo from NotificationParams that allows specifying a calling
138
+ * Context to be used for creating PendingIntents and one Context that the notification is
139
+ * intended for (resources, package name, manifest data, etc.)
140
+ */
141
+ public static DisplayNotificationInfo createNotificationInfo (
142
+ Context callingContext ,
143
+ Context appContext ,
144
+ NotificationParams params ,
145
+ String channelId ,
146
+ Bundle manifestMetadata ) {
147
+ String pkgName = appContext .getPackageName ();
148
+ Resources appResources = appContext .getResources ();
149
+ PackageManager appPackageManager = appContext .getPackageManager ();
150
+ return createNotificationInfo (
151
+ callingContext ,
152
+ appContext ,
153
+ params ,
154
+ channelId ,
155
+ manifestMetadata ,
156
+ pkgName ,
157
+ appResources ,
158
+ appPackageManager );
159
+ }
160
+
161
+ public static DisplayNotificationInfo createNotificationInfo (
162
+ Context callingContext ,
163
+ Context appContext ,
164
+ NotificationParams params ,
165
+ String channelId ,
166
+ Bundle manifestMetadata ,
167
+ String pkgName ,
168
+ Resources appResources ,
169
+ PackageManager appPackageManager ) {
170
+
171
+ NotificationCompat .Builder builder = new NotificationCompat .Builder (appContext , channelId );
123
172
124
173
String title =
125
174
params .getPossiblyLocalizedString (
@@ -150,15 +199,16 @@ public static DisplayNotificationInfo createNotificationInfo(
150
199
builder .setSound (sound );
151
200
}
152
201
153
- builder .setContentIntent (createContentIntent (context , params , pkgName , appPackageManager ));
202
+ builder .setContentIntent (
203
+ createContentIntent (callingContext , params , pkgName , appPackageManager ));
154
204
155
- PendingIntent deleteIntent = createDeleteIntent (context , params );
205
+ PendingIntent deleteIntent = createDeleteIntent (callingContext , appContext , params );
156
206
if (deleteIntent != null ) {
157
207
builder .setDeleteIntent (deleteIntent );
158
208
}
159
209
160
210
Integer color =
161
- getColor (context , params .getString (MessageNotificationKeys .COLOR ), manifestMetadata );
211
+ getColor (appContext , params .getString (MessageNotificationKeys .COLOR ), manifestMetadata );
162
212
if (color != null ) {
163
213
builder .setColor (color );
164
214
}
@@ -541,7 +591,8 @@ private static int getPendingIntentFlags(int baseFlags) {
541
591
}
542
592
543
593
@ Nullable
544
- private static PendingIntent createDeleteIntent (Context context , NotificationParams params ) {
594
+ private static PendingIntent createDeleteIntent (
595
+ Context callingContext , Context appContext , NotificationParams params ) {
545
596
if (!shouldUploadMetrics (params )) {
546
597
return null ;
547
598
}
@@ -550,17 +601,18 @@ private static PendingIntent createDeleteIntent(Context context, NotificationPar
550
601
new Intent (IntentActionKeys .NOTIFICATION_DISMISS )
551
602
.putExtras (params .paramsForAnalyticsIntent ());
552
603
553
- return createMessagingPendingIntent (context , dismissIntent );
604
+ return createMessagingPendingIntent (callingContext , appContext , dismissIntent );
554
605
}
555
606
556
607
/** Create a PendingIntent to start the app's messaging service via FirebaseInstanceIdReceiver */
557
- private static PendingIntent createMessagingPendingIntent (Context context , Intent intent ) {
608
+ private static PendingIntent createMessagingPendingIntent (
609
+ Context callingContext , Context appContext , Intent intent ) {
558
610
return PendingIntent .getBroadcast (
559
- context ,
611
+ callingContext ,
560
612
generatePendingIntentRequestCode (),
561
613
new Intent (ACTION_MESSAGING_EVENT )
562
614
.setComponent (
563
- new ComponentName (context , "com.google.firebase.iid.FirebaseInstanceIdReceiver" ))
615
+ new ComponentName (appContext , "com.google.firebase.iid.FirebaseInstanceIdReceiver" ))
564
616
.putExtra (IntentKeys .WRAPPED_INTENT , intent ),
565
617
getPendingIntentFlags (PendingIntent .FLAG_ONE_SHOT ));
566
618
}
0 commit comments