Skip to content
Open
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 @@ -157,11 +157,12 @@ void StartForegroundServices()
{
NotificationManager ??= GetSystemService(NotificationService) as NotificationManager ?? throw new InvalidOperationException($"{nameof(NotificationManager)} cannot be null");
notificationBuilder ??= new NotificationCompat.Builder(Platform.AppContext, "1");

var pendingIntent = CreateActivityPendingIntent();
notificationBuilder.SetSmallIcon(Resource.Drawable.media3_notification_small_icon);
notificationBuilder.SetAutoCancel(false);
notificationBuilder.SetForegroundServiceBehavior(NotificationCompat.ForegroundServiceImmediate);
notificationBuilder.SetVisibility(NotificationCompat.VisibilityPublic);
notificationBuilder.SetContentIntent(pendingIntent);

CreateNotificationChannel(NotificationManager);

Expand All @@ -177,4 +178,17 @@ void StartForegroundServices()
StartForeground(1, notificationBuilder.Build());
}
}

static PendingIntent CreateActivityPendingIntent()
{
var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");
var packageManager = Platform.AppContext.PackageManager ?? throw new InvalidOperationException("PackageManager cannot be null");
var launchIntent = packageManager.GetLaunchIntentForPackage(packageName) ?? throw new InvalidOperationException("Launch intent cannot be null");

launchIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);

var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags)
?? throw new InvalidOperationException("PendingIntent cannot be null");
}
}
Loading