Skip to content
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

Fix for sending scheduled local notifications #1458

Merged
merged 1 commit into from
Apr 23, 2024
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
2 changes: 1 addition & 1 deletion samples/Sample.Maui/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARMS" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARMS" android:maxSdkVersion="33" />
<uses-permission android:name="android.permission.USE_EXACT_ALARMS" />

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
Expand Down
34 changes: 17 additions & 17 deletions src/Shiny.Notifications/Platforms/Android/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,46 +138,46 @@ public async Task<AccessState> RequestAccess(AccessRequestFlags access)
public async Task Send(Notification notification)
{
notification.AssertValid();
var android = notification.TryToNative<AndroidNotification>();
var androidNotification = notification.TryToNative<AndroidNotification>();

// TODO: should I cancel an existing id if the user is setting it?
if (notification.Id == 0)
notification.Id = this.settings.IncrementValue("NotificationId");
if (androidNotification.Id == 0)
androidNotification.Id = this.settings.IncrementValue("NotificationId");

var channelId = notification.Channel ?? Channel.Default.Identifier;
var channelId = androidNotification.Channel ?? Channel.Default.Identifier;
var channel = this.channelManager.Get(channelId);
if (channel == null)
throw new InvalidProgramException("No channel found for " + channelId);

var builder = this.manager.CreateNativeBuilder(android, channel!);
var builder = this.manager.CreateNativeBuilder(androidNotification, channel!);

if (notification.Geofence != null)
if (androidNotification.Geofence != null)
{
await this.geofenceManager.StartMonitoring(new GeofenceRegion(
AndroidNotificationProcessor.GetGeofenceId(notification),
notification.Geofence!.Center!,
notification.Geofence!.Radius!
AndroidNotificationProcessor.GetGeofenceId(androidNotification),
androidNotification.Geofence!.Center!,
androidNotification.Geofence!.Radius!
));
}
else if (notification.RepeatInterval != null)
else if (androidNotification.RepeatInterval != null)
{
// calc first date if repeating interval
notification.ScheduleDate = notification.RepeatInterval!.CalculateNextAlarm();
androidNotification.ScheduleDate = androidNotification.RepeatInterval!.CalculateNextAlarm();
}

if (notification.ScheduleDate == null && notification.Geofence == null)
if (androidNotification.ScheduleDate == null && androidNotification.Geofence == null)
{
var native = builder.Build();
this.manager.NativeManager.Notify(notification.Id, native);
this.manager.NativeManager.Notify(androidNotification.Id, native);
}
else
{
// ensure a channel is set
notification.Channel = channel!.Identifier;
this.repository.Set(notification);
androidNotification.Channel = channel!.Identifier;
this.repository.Set(androidNotification);

if (notification.ScheduleDate != null)
this.manager.SetAlarm(notification);
if (androidNotification.ScheduleDate != null)
this.manager.SetAlarm(androidNotification);
}
}

Expand Down