Skip to content
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");
Comment on lines +184 to +186
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring these null checks to use pattern matching for consistency with the codebase standards.

For example, instead of:

var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");

Use:

if (Platform.AppContext.PackageName is not { } packageName)
{
    throw new InvalidOperationException("PackageName cannot be null");
}

This pattern should be applied to all three null checks in this method (PackageName, PackageManager, and the launch intent).

Copilot generated this review using guidance from repository custom instructions.

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

var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PendingIntentFlags.Immutable flag was introduced in Android 12 (API 31), but this service supports Android 26+ (as indicated by the [SupportedOSPlatform("Android26.0")] attribute). Using this flag unconditionally will cause runtime errors on Android versions 26-30.

Consider adding a version check to use the appropriate flags:

var flags = OperatingSystem.IsAndroidVersionAtLeast(31)
    ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
    : PendingIntentFlags.UpdateCurrent;

Alternatively, if Android 12+ is now the minimum supported version, update the SupportedOSPlatform attribute on the class accordingly.

Suggested change
var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
var flags = OperatingSystem.IsAndroidVersionAtLeast(31)
? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
: PendingIntentFlags.UpdateCurrent;

Copilot uses AI. Check for mistakes.
return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags)
?? throw new InvalidOperationException("PendingIntent cannot be null");
Comment on lines +191 to +192
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring this null check to use pattern matching for consistency with the codebase standards.

For example, instead of:

return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) ?? throw new InvalidOperationException("PendingIntent cannot be null");

Use:

if (PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) is not { } pendingIntent)
{
    throw new InvalidOperationException("PendingIntent cannot be null");
}
return pendingIntent;

Copilot generated this review using guidance from repository custom instructions.
}
}
Loading