Skip to content

Add createNotificationInfo() that takes a calling and an app Context. #3598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,25 @@ public final class CommonNotificationBuilder {
// Do not instantiate.
private CommonNotificationBuilder() {}

/** Creates a DisplayNotificationInfo from NotificationParams for a single Context. */
static DisplayNotificationInfo createNotificationInfo(
Context context, NotificationParams params) {
Bundle manifestMetadata =
getManifestMetadata(context.getPackageManager(), context.getPackageName());

return createNotificationInfo(
context,
context.getPackageName(),
context,
params,
getOrCreateChannel(context, params.getNotificationChannelId(), manifestMetadata),
context.getResources(),
context.getPackageManager(),
manifestMetadata);
}

/**
* Legacy method that creates a DisplayNotificationInfo from NotificationParams that allows
* specifying components so the calling Context can be different from the Context used for the
* notification (resources, etc.).
*/
public static DisplayNotificationInfo createNotificationInfo(
Context context,
String pkgName,
Expand All @@ -118,8 +122,53 @@ public static DisplayNotificationInfo createNotificationInfo(
Resources appResources,
PackageManager appPackageManager,
Bundle manifestMetadata) {
return createNotificationInfo(
context,
context,
params,
channelId,
manifestMetadata,
pkgName,
appResources,
appPackageManager);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
/**
* Creates a DisplayNotificationInfo from NotificationParams 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.)
*/
public static DisplayNotificationInfo createNotificationInfo(
Context callingContext,
Context appContext,
NotificationParams params,
String channelId,
Bundle manifestMetadata) {
String pkgName = appContext.getPackageName();
Resources appResources = appContext.getResources();
PackageManager appPackageManager = appContext.getPackageManager();
return createNotificationInfo(
callingContext,
appContext,
params,
channelId,
manifestMetadata,
pkgName,
appResources,
appPackageManager);
}

public static DisplayNotificationInfo createNotificationInfo(
Context callingContext,
Context appContext,
NotificationParams params,
String channelId,
Bundle manifestMetadata,
String pkgName,
Resources appResources,
PackageManager appPackageManager) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext, channelId);

String title =
params.getPossiblyLocalizedString(
Expand Down Expand Up @@ -150,15 +199,16 @@ public static DisplayNotificationInfo createNotificationInfo(
builder.setSound(sound);
}

builder.setContentIntent(createContentIntent(context, params, pkgName, appPackageManager));
builder.setContentIntent(
createContentIntent(callingContext, params, pkgName, appPackageManager));

PendingIntent deleteIntent = createDeleteIntent(context, params);
PendingIntent deleteIntent = createDeleteIntent(callingContext, appContext, params);
if (deleteIntent != null) {
builder.setDeleteIntent(deleteIntent);
}

Integer color =
getColor(context, params.getString(MessageNotificationKeys.COLOR), manifestMetadata);
getColor(appContext, params.getString(MessageNotificationKeys.COLOR), manifestMetadata);
if (color != null) {
builder.setColor(color);
}
Expand Down Expand Up @@ -541,7 +591,8 @@ private static int getPendingIntentFlags(int baseFlags) {
}

@Nullable
private static PendingIntent createDeleteIntent(Context context, NotificationParams params) {
private static PendingIntent createDeleteIntent(
Context callingContext, Context appContext, NotificationParams params) {
if (!shouldUploadMetrics(params)) {
return null;
}
Expand All @@ -550,17 +601,18 @@ private static PendingIntent createDeleteIntent(Context context, NotificationPar
new Intent(IntentActionKeys.NOTIFICATION_DISMISS)
.putExtras(params.paramsForAnalyticsIntent());

return createMessagingPendingIntent(context, dismissIntent);
return createMessagingPendingIntent(callingContext, appContext, dismissIntent);
}

/** Create a PendingIntent to start the app's messaging service via FirebaseInstanceIdReceiver */
private static PendingIntent createMessagingPendingIntent(Context context, Intent intent) {
private static PendingIntent createMessagingPendingIntent(
Context callingContext, Context appContext, Intent intent) {
return PendingIntent.getBroadcast(
context,
callingContext,
generatePendingIntentRequestCode(),
new Intent(ACTION_MESSAGING_EVENT)
.setComponent(
new ComponentName(context, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
new ComponentName(appContext, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
.putExtra(IntentKeys.WRAPPED_INTENT, intent),
getPendingIntentFlags(PendingIntent.FLAG_ONE_SHOT));
}
Expand Down
Loading