Skip to content

Commit

Permalink
Move static properties from Android's FIrebasePushNotificationManager…
Browse files Browse the repository at this point in the history
… to FirebasePushNotificationAndroidOptions
  • Loading branch information
Thomas Galliker committed Oct 1, 2024
1 parent 78897f6 commit 8b3c024
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public abstract class FirebasePushNotificationManagerBase : IDisposable
private readonly string instanceId = Guid.NewGuid().ToString()[..5];
protected readonly ILogger logger;
private readonly ILoggerFactory loggerFactory;
protected readonly FirebasePushNotificationOptions options;
protected readonly IFirebasePushNotificationPreferences preferences;

private string[] subscribedTopics;
Expand Down Expand Up @@ -39,6 +40,7 @@ protected internal FirebasePushNotificationManagerBase(
{
this.logger = logger;
this.loggerFactory = loggerFactory;
this.options = options;
this.preferences = preferences;

this.CreateOrUpdateQueues(options.QueueFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ namespace Plugin.FirebasePushNotifications.Platforms
[Preserve(AllMembers = true)]
public class FirebasePushNotificationManager : FirebasePushNotificationManagerBase, IFirebasePushNotification
{
[Obsolete("Will be move to FirebasePushNotificationOptions.Android soon")]
public static Android.Net.Uri SoundUri { get; set; }

[Obsolete("Will be move to FirebasePushNotificationOptions.Android soon")]
public static Type NotificationActivityType { get; set; }

[Obsolete("Will be move to FirebasePushNotificationOptions.Android soon")]
public static ActivityFlags? NotificationActivityFlags { get; set; } = ActivityFlags.ClearTop | ActivityFlags.SingleTop;

[Obsolete("Will be move to FirebasePushNotificationOptions.Android soon")]
internal static Type DefaultNotificationActivityType { get; set; } = null;

internal FirebasePushNotificationManager(
ILogger<FirebasePushNotificationManager> logger,
ILoggerFactory loggerFactory,
Expand All @@ -37,12 +25,12 @@ internal FirebasePushNotificationManager(
: base(logger, loggerFactory, options, pushNotificationHandler, preferences)
{
this.NotificationBuilder = notificationBuilder;
this.ConfigurePlatform(options);
this.ConfigurePlatform();
}

private void ConfigurePlatform(FirebasePushNotificationOptions options)
private void ConfigurePlatform()
{
if (options.AutoInitEnabled)
if (this.options.AutoInitEnabled)
{
try
{
Expand All @@ -57,13 +45,11 @@ private void ConfigurePlatform(FirebasePushNotificationOptions options)
}
}

FirebaseMessaging.Instance.AutoInitEnabled = options.AutoInitEnabled;

NotificationActivityType = options.Android.NotificationActivityType;
FirebaseMessaging.Instance.AutoInitEnabled = this.options.AutoInitEnabled;

var notificationChannels = NotificationChannels.Current;

var notificationChannelRequests = options.Android.NotificationChannels.ToArray();
var notificationChannelRequests = this.options.Android.NotificationChannels.ToArray();
if (notificationChannelRequests.Length == 0)
{
notificationChannelRequests = new[] { Constants.DefaultNotificationChannel };
Expand All @@ -86,14 +72,14 @@ public void ProcessIntent(Activity activity, Intent intent)

var activityType = activity.GetType();

// Initialize NotificationActivityType in case it was left null
// in FirebasePushNotificationOptions.Android.NotificationActivityType.
if (NotificationActivityType == null && typeof(MauiAppCompatActivity).IsAssignableFrom(activityType))
if (this.options.Android.NotificationActivityType == null && typeof(MauiAppCompatActivity).IsAssignableFrom(activityType))
{
NotificationActivityType = activityType;
// Initialize NotificationActivityType in case it was left null
// in FirebasePushNotificationAndroidOptions.NotificationActivityType.
this.options.Android.NotificationActivityType = activityType;
}

if (NotificationActivityType != activityType)
if (this.options.Android.NotificationActivityType != activityType)
{
return;
}
Expand All @@ -116,14 +102,14 @@ public void ProcessIntent(Activity activity, Intent intent)
if (extras.Any())
{
// Don't process old/historic intents which are recycled for whatever reason
var intentAlreadyHandledKey = Constants.ExtraFirebaseProcessIntentHandled;
const string intentAlreadyHandledKey = Constants.ExtraFirebaseProcessIntentHandled;
if (!intent.GetBooleanExtra(intentAlreadyHandledKey, false))
{
intent.PutExtra(intentAlreadyHandledKey, true);
this.logger.LogDebug($"ProcessIntent: {intentAlreadyHandledKey} not present --> Process notification");

// TODO: Refactor this! This is for sure not a good behavior..
DefaultNotificationActivityType = activityType;
// TODO: Refactor this! This is for sure not a good behavior!!
this.options.Android.DefaultNotificationActivityType = activityType;

var notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public virtual void OnNotificationReceived(IDictionary<string, object> data)
// Long term goal: A developer can use IPushNotificationHandler to intercept all notifications and do some operations on them.
// All the logic in here should move to the Android-specific implementation of FirebasePushNotificationManager.

var context = Application.Context;

var resultIntent = CreateActivityLaunchIntent(context);

var extras = new Bundle();
foreach (var kvp in data)
{
Expand All @@ -120,9 +116,11 @@ public virtual void OnNotificationReceived(IDictionary<string, object> data)
extras.PutString(Constants.ActionNotificationTagKey, tag);
}

var context = Application.Context;
var resultIntent = this.CreateActivityLaunchIntent(context);
resultIntent.PutExtras(extras);

if (FirebasePushNotificationManager.NotificationActivityFlags is ActivityFlags activityFlags)
if (this.options.Android.NotificationActivityFlags is ActivityFlags activityFlags)
{
resultIntent.SetFlags(activityFlags);
}
Expand Down Expand Up @@ -256,12 +254,12 @@ public virtual void OnNotificationReceived(IDictionary<string, object> data)
PendingIntent pendingActionIntent;
if (notificationAction.Type == NotificationActionType.Foreground)
{
actionIntent = CreateActivityLaunchIntent(context);
actionIntent = this.CreateActivityLaunchIntent(context);
actionIntent.PutExtras(extras);

if (FirebasePushNotificationManager.NotificationActivityFlags != null)
if (this.options.Android.NotificationActivityFlags is ActivityFlags intentActivityFlags)
{
actionIntent.SetFlags(FirebasePushNotificationManager.NotificationActivityFlags.Value);
actionIntent.SetFlags(intentActivityFlags);
}

pendingActionIntent = PendingIntent.GetActivity(context, aRequestCode, actionIntent,
Expand Down Expand Up @@ -339,7 +337,7 @@ private NotificationCompat.Style GetNotificationStyle(IDictionary<string, object

private Uri GetSoundUri(IDictionary<string, object> data, Context context)
{
var soundUri = FirebasePushNotificationManager.SoundUri;
var soundUri = this.options.Android.SoundUri;

try
{
Expand Down Expand Up @@ -593,19 +591,21 @@ private static Bundle GetMetadata()
return metadata;
}

private static Intent CreateActivityLaunchIntent(Context context)
private Intent CreateActivityLaunchIntent(Context context)
{
Intent activityIntent;

if (typeof(Activity).IsAssignableFrom(FirebasePushNotificationManager.NotificationActivityType))
if (this.options.Android.NotificationActivityType is Type notificationActivityType)
{
activityIntent = new Intent(context, notificationActivityType);
}
else if (this.options.Android.DefaultNotificationActivityType is Type defaultNotificationActivityType)
{
activityIntent = new Intent(context, FirebasePushNotificationManager.NotificationActivityType);
activityIntent = new Intent(context, defaultNotificationActivityType);
}
else
{
activityIntent = FirebasePushNotificationManager.DefaultNotificationActivityType == null
? context.PackageManager.GetLaunchIntentForPackage(context.PackageName)
: new Intent(context, FirebasePushNotificationManager.DefaultNotificationActivityType);
activityIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
}

return activityIntent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if ANDROID
using Android.App;
using Android.Content;
using Plugin.FirebasePushNotifications.Platforms.Channels;

namespace Plugin.FirebasePushNotifications.Platforms
Expand All @@ -13,6 +14,7 @@ public class FirebasePushNotificationAndroidOptions
/// The Activity which handles incoming push notifications.
/// Typically, this is <c>typeof(MainActivity)</c>.
/// </summary>
/// <exception cref="ArgumentException">If given type is not an Activity.</exception>
public virtual Type NotificationActivityType
{
get => this.notificationActivityType;
Expand Down Expand Up @@ -91,6 +93,12 @@ internal static void EnsureNotificationChannelRequests(NotificationChannelReques

public Android.Graphics.Color? DefaultColor { get; set; }

public ActivityFlags? NotificationActivityFlags { get; set; } = ActivityFlags.ClearTop | ActivityFlags.SingleTop;

internal Type DefaultNotificationActivityType { get; set; } = null;

public Android.Net.Uri SoundUri { get; set; }

public bool ShouldShowWhen { get; set; } = true;

public bool UseBigTextStyle { get; set; } = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal FirebasePushNotificationManager(
IFirebasePushNotificationPreferences preferences)
: base(logger, loggerFactory, options, pushNotificationHandler, preferences)
{
this.ConfigurePlatform(options);
this.ConfigurePlatform();
}

/// <inheritdoc />
Expand Down Expand Up @@ -111,7 +111,7 @@ private static UNNotificationActionOptions GetUNNotificationActionOptions(Notifi
return notificationActionType;
}

private void ConfigurePlatform(FirebasePushNotificationOptions options)
private void ConfigurePlatform()
{
if (Firebase.Core.App.DefaultInstance == null)
{
Expand Down

0 comments on commit 8b3c024

Please sign in to comment.