From 1ae388cb0382c93ce1a21ea2f96f1a7ba281bfc1 Mon Sep 17 00:00:00 2001 From: Dinis Vieira Date: Tue, 13 Feb 2024 22:07:09 +0000 Subject: [PATCH] [PM-5900] Fix for Device Login push notifications cause the app to show HomePage (#2989) * PM-5900 Fix for Device Login push notifications cause the app to show the HomePage * PM-5900 Added edge case scenario if intent / packageName / packageManager is null --- .../AndroidPushNotificationService.cs | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/App/Platforms/Android/Services/AndroidPushNotificationService.cs b/src/App/Platforms/Android/Services/AndroidPushNotificationService.cs index 2492318bc..30187a161 100644 --- a/src/App/Platforms/Android/Services/AndroidPushNotificationService.cs +++ b/src/App/Platforms/Android/Services/AndroidPushNotificationService.cs @@ -79,24 +79,29 @@ namespace Bit.Droid.Services } var context = Android.App.Application.Context; - var intent = new Intent(context, typeof(MainActivity)); - intent.PutExtra(Bit.Core.Constants.NotificationData, JsonConvert.SerializeObject(data)); - var pendingIntentFlags = AndroidHelpers.AddPendingIntentMutabilityFlag(PendingIntentFlags.UpdateCurrent, true); - var pendingIntent = PendingIntent.GetActivity(context, 20220801, intent, pendingIntentFlags); + var intent = context.PackageManager?.GetLaunchIntentForPackage(context.PackageName ?? string.Empty); - var deleteIntent = new Intent(context, typeof(NotificationDismissReceiver)); - deleteIntent.PutExtra(Bit.Core.Constants.NotificationData, JsonConvert.SerializeObject(data)); - var deletePendingIntent = PendingIntent.GetBroadcast(context, 20220802, deleteIntent, pendingIntentFlags); + var builder = new NotificationCompat.Builder(context, Bit.Core.Constants.AndroidNotificationChannelId); + if(intent != null && context.PackageManager != null && !string.IsNullOrEmpty(context.PackageName)) + { + intent.PutExtra(Bit.Core.Constants.NotificationData, JsonConvert.SerializeObject(data)); + var pendingIntentFlags = AndroidHelpers.AddPendingIntentMutabilityFlag(PendingIntentFlags.UpdateCurrent, true); + var pendingIntent = PendingIntent.GetActivity(context, 20220801, intent, pendingIntentFlags); - var builder = new NotificationCompat.Builder(context, Bit.Core.Constants.AndroidNotificationChannelId) - .SetContentIntent(pendingIntent) - .SetContentTitle(title) + var deleteIntent = new Intent(context, typeof(NotificationDismissReceiver)); + deleteIntent.PutExtra(Bit.Core.Constants.NotificationData, JsonConvert.SerializeObject(data)); + var deletePendingIntent = PendingIntent.GetBroadcast(context, 20220802, deleteIntent, pendingIntentFlags); + + builder.SetContentIntent(pendingIntent) + .SetDeleteIntent(deletePendingIntent); + } + + builder.SetContentTitle(title) .SetContentText(message) .SetSmallIcon(Bit.Core.Resource.Drawable.ic_notification) .SetColor((int)Android.Graphics.Color.White) - .SetDeleteIntent(deletePendingIntent) .SetAutoCancel(true); - + if (data is PasswordlessNotificationData passwordlessNotificationData && passwordlessNotificationData.TimeoutInMinutes > 0) { builder.SetTimeoutAfter(passwordlessNotificationData.TimeoutInMinutes * 60000);