Skip to content

Commit 642ce19

Browse files
committed
Added support request Schedule Exact Alarm
1 parent e1fbd86 commit 642ce19

File tree

9 files changed

+116
-47
lines changed

9 files changed

+116
-47
lines changed

Diff for: Sample/Direct Maui/LocalNotification.Sample/LocalNotification.Sample.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<UseMaui>true</UseMaui>
1111
<SingleProject>true</SingleProject>
1212
<ImplicitUsings>enable</ImplicitUsings>
13-
<Nullable>enable</Nullable>
13+
<Nullable>enable</Nullable>
1414

1515
<!-- Display name -->
1616
<ApplicationTitle>LocalNotification.Sample</ApplicationTitle>
@@ -52,6 +52,7 @@
5252
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
5353
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
5454
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
55+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
5556
</ItemGroup>
5657

5758
<ItemGroup>

Diff for: Sample/Direct Maui/LocalNotification.Sample/MainPage.xaml.cs

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using Plugin.LocalNotification;
1+
#if ANDROID
2+
using Android.App;
3+
using Android.Content;
4+
#endif
5+
using Microsoft.Maui.Controls.PlatformConfiguration;
6+
using Plugin.LocalNotification;
27
using Plugin.LocalNotification.AndroidOption;
38
using Plugin.LocalNotification.EventArgs;
49
using System.Text;
@@ -174,17 +179,40 @@ private async void Button_Clicked(object sender, EventArgs e)
174179

175180
try
176181
{
182+
//var permissionRequest = new NotificationPermission
183+
//{
184+
// Android =
185+
// {
186+
// RequestPermissionToScheduleExactAlarm = true
187+
// }
188+
//};
189+
190+
//if (await _notificationService.AreNotificationsEnabled(permissionRequest) == false)
191+
//{
192+
// await _notificationService.RequestNotificationPermission(permissionRequest);
193+
//}
194+
177195
if (await _notificationService.AreNotificationsEnabled() == false)
178196
{
179-
await _notificationService.RequestNotificationPermission(new NotificationPermission
180-
{
181-
Android =
182-
{
183-
RequestPermissionToScheduleExactAlarm = true
184-
}
185-
});
197+
await _notificationService.RequestNotificationPermission();
186198
}
187199

200+
//#if ANDROID
201+
202+
// var myAlarmManager = AlarmManager.FromContext(Android.App.Application.Context);
203+
// var canScheduleExactAlarms = myAlarmManager?.CanScheduleExactAlarms() ?? false;
204+
205+
// if (!canScheduleExactAlarms)
206+
// {
207+
// var uri = Android.Net.Uri.Parse($"package:{Android.App.Application.Context.PackageName}");
208+
// var intent = new Intent(Android.Provider.Settings.ActionRequestScheduleExactAlarm, uri);
209+
// Android.App.Application.Context.StartActivity(intent);
210+
211+
// canScheduleExactAlarms = myAlarmManager?.CanScheduleExactAlarms() ?? false;
212+
// }
213+
214+
//#endif
215+
188216
var ff = await _notificationService.Show(request);
189217

190218
//var sn = ToastNotificationManagerCompat.CreateToastNotifier().GetScheduledToastNotifications();

Diff for: Sample/Direct Maui/LocalNotification.Sample/MauiProgram.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public static MauiApp CreateMauiApp()
8282

8383

8484
#if DEBUG
85-
builder.Logging.AddDebug();
85+
LocalNotificationCenter.LogLevel = LogLevel.Debug;
86+
//builder.Logging.AddDebug();
87+
builder.Logging.AddConsole();
8688
#endif
8789

8890
builder.Services.AddTransient<MainPage>();

Diff for: Sample/Direct Maui/LocalNotification.Sample/Platforms/Android/ManifestInfo.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
88
[assembly: UsesPermission(Manifest.Permission.Vibrate)]
99

10-
[assembly: UsesPermission(Manifest.Permission.ScheduleExactAlarm)]
1110
[assembly: UsesPermission(Manifest.Permission.PostNotifications)]

Diff for: Source/Plugin.LocalNotification/INotificationService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public interface INotificationService
9999
/// Returns whether user as allowed Notifications
100100
/// </summary>
101101
/// <returns></returns>
102-
Task<bool> AreNotificationsEnabled();
102+
Task<bool> AreNotificationsEnabled(NotificationPermission? permission = null);
103103

104104
/// <summary>
105105
/// Request Notification Permission

Diff for: Source/Plugin.LocalNotification/Platforms/Android/NotificationServiceImpl.cs

+56-20
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,7 @@ internal virtual async Task<bool> ShowNow(NotificationRequest request)
605605

606606
return true;
607607
}
608-
609-
/// <inheritdoc />
610-
public Task<bool> AreNotificationsEnabled()
611-
{
612-
return MyNotificationManager is null
613-
? Task.FromResult(false)
614-
: !OperatingSystem.IsAndroidVersionAtLeast(24)
615-
? Task.FromResult(true)
616-
: Task.FromResult(MyNotificationManager.AreNotificationsEnabled());
617-
}
618-
608+
619609
/// <summary>
620610
///
621611
/// </summary>
@@ -787,41 +777,87 @@ public void RegisterCategoryList(HashSet<NotificationCategory> categoryList)
787777
}
788778
}
789779

780+
private NotificationPermission _notificationPermission = new();
781+
782+
/// <inheritdoc />
783+
public Task<bool> AreNotificationsEnabled(NotificationPermission? permission = null)
784+
{
785+
_notificationPermission = permission is not null ? permission : new NotificationPermission();
786+
787+
if (MyNotificationManager is null)
788+
{
789+
return Task.FromResult(false);
790+
}
791+
792+
if(!OperatingSystem.IsAndroidVersionAtLeast(24))
793+
{
794+
return Task.FromResult(true);
795+
}
796+
797+
if(!MyNotificationManager.AreNotificationsEnabled())
798+
{
799+
return Task.FromResult(false);
800+
}
801+
802+
if (!OperatingSystem.IsAndroidVersionAtLeast(33))
803+
{
804+
return Task.FromResult(true);
805+
}
806+
807+
if (_notificationPermission.Android.RequestPermissionToScheduleExactAlarm)
808+
{
809+
var canScheduleExactAlarms = MyAlarmManager?.CanScheduleExactAlarms() ?? false;
810+
811+
return Task.FromResult(canScheduleExactAlarms);
812+
}
813+
814+
return Task.FromResult(true);
815+
}
816+
790817
/// <inheritdoc />
791818
public async Task<bool> RequestNotificationPermission(NotificationPermission? permission = null)
792819
{
793-
permission ??= new NotificationPermission();
820+
_notificationPermission = permission is not null ? permission : new NotificationPermission();
821+
822+
var allowed = await AreNotificationsEnabled(_notificationPermission);
823+
if (allowed)
824+
{
825+
return true;
826+
}
794827

795828
if (!OperatingSystem.IsAndroidVersionAtLeast(33))
796829
{
797-
return false;
830+
return true;
798831
}
799832

800-
if (!permission.AskPermission)
833+
if (!_notificationPermission.AskPermission)
801834
{
802835
return false;
803836
}
804-
837+
805838
var status = await Permissions.RequestAsync<NotificationPerms>();
806-
if(status != PermissionStatus.Granted)
839+
if (status != PermissionStatus.Granted)
807840
{
808841
return false;
809842
}
810843

811-
if (!permission.Android.RequestPermissionToScheduleExactAlarm)
844+
LocalNotificationCenter.Log($"Request Permission To Schedule Exact Alarm: {_notificationPermission.Android.RequestPermissionToScheduleExactAlarm}");
845+
846+
if (!_notificationPermission.Android.RequestPermissionToScheduleExactAlarm)
812847
{
813848
return true;
814849
}
815-
850+
816851
var canScheduleExactAlarms = MyAlarmManager?.CanScheduleExactAlarms() ?? false;
817852

853+
LocalNotificationCenter.Log($"Can Schedule Exact Alarms: {canScheduleExactAlarms}");
854+
818855
if (!canScheduleExactAlarms)
819856
{
820857
var uri = Android.Net.Uri.Parse($"package:{Application.Context.PackageName}");
821858
var intent = new Intent(Android.Provider.Settings.ActionRequestScheduleExactAlarm, uri);
859+
intent.AddFlags(ActivityFlags.NewTask);
822860
Application.Context.StartActivity(intent);
823-
824-
canScheduleExactAlarms = MyAlarmManager?.CanScheduleExactAlarms() ?? false;
825861
}
826862

827863
return canScheduleExactAlarms;

Diff for: Source/Plugin.LocalNotification/Platforms/Windows/NotificationServiceImpl.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ public NotificationServiceImpl()
2424
_notifier = ToastNotificationManagerCompat.CreateToastNotifier();
2525
}
2626

27-
public Task<bool> AreNotificationsEnabled()
28-
{
29-
return _notifier.Setting == NotificationSetting.Enabled ? Task.FromResult(true) : Task.FromResult(false);
30-
}
31-
3227
public bool Cancel(params int[] notificationIdList)
3328
{
3429
var scheduledToasts = _notifier.GetScheduledToastNotifications();
@@ -154,6 +149,11 @@ public void RegisterCategoryList(HashSet<NotificationCategory> categoryList)
154149
}
155150
}
156151

152+
public Task<bool> AreNotificationsEnabled(NotificationPermission? permission = null)
153+
{
154+
return _notifier.Setting == NotificationSetting.Enabled ? Task.FromResult(true) : Task.FromResult(false);
155+
}
156+
157157
public Task<bool> RequestNotificationPermission(NotificationPermission? permission = null)
158158
{
159159
return LocalNotificationCenter.RequestNotificationPermissionAsync(permission);

Diff for: Source/Plugin.LocalNotification/Platforms/iOS/NotificationServiceImpl.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ await UNUserNotificationCenter.Current.AddNotificationRequestAsync(nativeRequest
154154
}
155155
}
156156

157-
/// <inheritdoc />
158-
public async Task<bool> AreNotificationsEnabled()
159-
{
160-
var settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync().ConfigureAwait(false);
161-
return settings.AlertSetting == UNNotificationSetting.Enabled;
162-
}
163-
164157
/// <summary>
165158
///
166159
/// </summary>
@@ -415,7 +408,14 @@ public async Task<IList<NotificationRequest>> GetDeliveredNotificationList()
415408

416409
return delivered.Select(r => LocalNotificationCenter.GetRequest(r.Request.Content) ?? new NotificationRequest()).ToList();
417410
}
418-
411+
412+
/// <inheritdoc />
413+
public async Task<bool> AreNotificationsEnabled(NotificationPermission? permission = null)
414+
{
415+
var settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync().ConfigureAwait(false);
416+
return settings.AlertSetting == UNNotificationSetting.Enabled;
417+
}
418+
419419
/// <inheritdoc />
420420
public async Task<bool> RequestNotificationPermission(NotificationPermission? permission = null)
421421
{
@@ -428,7 +428,7 @@ public async Task<bool> RequestNotificationPermission(NotificationPermission? pe
428428
return false;
429429
}
430430

431-
var allowed = await AreNotificationsEnabled();
431+
var allowed = await AreNotificationsEnabled(permission);
432432
if (allowed)
433433
{
434434
return true;

Diff for: Source/Plugin.LocalNotification/Plugin.LocalNotification.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
1010

1111
<Title>$(AssemblyName)</Title>
12-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1312
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1413
<Authors>Elvin (Tharindu) Thudugala</Authors>
1514
<PackageTags>dotnet;android;ios;local;notification;local.notification;maui</PackageTags>
@@ -50,6 +49,10 @@
5049
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
5150
</PropertyGroup>
5251

52+
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
53+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
54+
</PropertyGroup>
55+
5356
<ItemGroup>
5457
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
5558
</ItemGroup>

0 commit comments

Comments
 (0)