Skip to content

Commit 0472f6f

Browse files
authored
Add createNotificationInfo() that takes a calling and an app Context. (#3598)
* Added createNotificationInfo() variant that allows specifying a calling Context to be used for creating PendingIntents and one Context that the notification is intended for (resources, package name, manifest data, etc.).
1 parent c1d517c commit 0472f6f

File tree

2 files changed

+211
-58
lines changed

2 files changed

+211
-58
lines changed

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

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,25 @@ public final class CommonNotificationBuilder {
9595
// Do not instantiate.
9696
private CommonNotificationBuilder() {}
9797

98+
/** Creates a DisplayNotificationInfo from NotificationParams for a single Context. */
9899
static DisplayNotificationInfo createNotificationInfo(
99100
Context context, NotificationParams params) {
100101
Bundle manifestMetadata =
101102
getManifestMetadata(context.getPackageManager(), context.getPackageName());
102103

103104
return createNotificationInfo(
104105
context,
105-
context.getPackageName(),
106+
context,
106107
params,
107108
getOrCreateChannel(context, params.getNotificationChannelId(), manifestMetadata),
108-
context.getResources(),
109-
context.getPackageManager(),
110109
manifestMetadata);
111110
}
112111

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+
*/
113117
public static DisplayNotificationInfo createNotificationInfo(
114118
Context context,
115119
String pkgName,
@@ -118,8 +122,53 @@ public static DisplayNotificationInfo createNotificationInfo(
118122
Resources appResources,
119123
PackageManager appPackageManager,
120124
Bundle manifestMetadata) {
125+
return createNotificationInfo(
126+
context,
127+
context,
128+
params,
129+
channelId,
130+
manifestMetadata,
131+
pkgName,
132+
appResources,
133+
appPackageManager);
134+
}
121135

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);
123172

124173
String title =
125174
params.getPossiblyLocalizedString(
@@ -150,15 +199,16 @@ public static DisplayNotificationInfo createNotificationInfo(
150199
builder.setSound(sound);
151200
}
152201

153-
builder.setContentIntent(createContentIntent(context, params, pkgName, appPackageManager));
202+
builder.setContentIntent(
203+
createContentIntent(callingContext, params, pkgName, appPackageManager));
154204

155-
PendingIntent deleteIntent = createDeleteIntent(context, params);
205+
PendingIntent deleteIntent = createDeleteIntent(callingContext, appContext, params);
156206
if (deleteIntent != null) {
157207
builder.setDeleteIntent(deleteIntent);
158208
}
159209

160210
Integer color =
161-
getColor(context, params.getString(MessageNotificationKeys.COLOR), manifestMetadata);
211+
getColor(appContext, params.getString(MessageNotificationKeys.COLOR), manifestMetadata);
162212
if (color != null) {
163213
builder.setColor(color);
164214
}
@@ -541,7 +591,8 @@ private static int getPendingIntentFlags(int baseFlags) {
541591
}
542592

543593
@Nullable
544-
private static PendingIntent createDeleteIntent(Context context, NotificationParams params) {
594+
private static PendingIntent createDeleteIntent(
595+
Context callingContext, Context appContext, NotificationParams params) {
545596
if (!shouldUploadMetrics(params)) {
546597
return null;
547598
}
@@ -550,17 +601,18 @@ private static PendingIntent createDeleteIntent(Context context, NotificationPar
550601
new Intent(IntentActionKeys.NOTIFICATION_DISMISS)
551602
.putExtras(params.paramsForAnalyticsIntent());
552603

553-
return createMessagingPendingIntent(context, dismissIntent);
604+
return createMessagingPendingIntent(callingContext, appContext, dismissIntent);
554605
}
555606

556607
/** 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) {
558610
return PendingIntent.getBroadcast(
559-
context,
611+
callingContext,
560612
generatePendingIntentRequestCode(),
561613
new Intent(ACTION_MESSAGING_EVENT)
562614
.setComponent(
563-
new ComponentName(context, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
615+
new ComponentName(appContext, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
564616
.putExtra(IntentKeys.WRAPPED_INTENT, intent),
565617
getPendingIntentFlags(PendingIntent.FLAG_ONE_SHOT));
566618
}

0 commit comments

Comments
 (0)