Skip to content

Commit e5f3f56

Browse files
committed
Fixed Xamarin permission issue
1 parent d9297d5 commit e5f3f56

File tree

8 files changed

+62
-21
lines changed

8 files changed

+62
-21
lines changed

Sample/Direct Maui/LocalNotification.Sample/LocalNotification.Sample.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
<BundleResource Include="Platforms\iOS\Resources\good_things_happen.aiff" Link="Resources\good_things_happen.aiff" />
4848
</ItemGroup>
4949

50+
<ItemGroup>
51+
<None Remove="Resources\appicon.png" />
52+
</ItemGroup>
53+
54+
<ItemGroup>
55+
<EmbeddedResource Include="Resources\appicon.png" />
56+
</ItemGroup>
57+
5058
<ItemGroup>
5159
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
5260
</ItemGroup>

Sample/Direct Maui/LocalNotification.Sample/MainPage.xaml.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public MainPage(INotificationService notificationService)
1717

1818
_notificationService = notificationService;
1919

20-
_notificationService.NotificationReceiving = OnNotificationReceiving;
20+
//_notificationService.NotificationReceiving = OnNotificationReceiving;
2121
_notificationService.NotificationReceived += ShowCustomAlertFromNotification;
2222
_notificationService.NotificationActionTapped += Current_NotificationActionTapped;
2323

2424
NotifyDatePicker.MinimumDate = DateTime.Today;
2525
NotifyTimePicker.Time = DateTime.Now.TimeOfDay.Add(TimeSpan.FromSeconds(10));
2626

27-
ScheduleNotificationGroup();
28-
ScheduleNotification("first", 10);
27+
//ScheduleNotificationGroup();
28+
//ScheduleNotification("first", 10);
2929
//ScheduleNotification("second", 20);
3030

3131
_cacheFilePath = FileSystem.Current.CacheDirectory + $"/testFile.txt";
@@ -64,16 +64,16 @@ private async Task LoadText()
6464
TestFileText.Text = fileText ?? "No Text";
6565
}
6666

67-
private Task<NotificationEventReceivingArgs> OnNotificationReceiving(NotificationRequest request)
68-
{
69-
request.Title = $"{request.Title} Modified";
67+
//private Task<NotificationEventReceivingArgs> OnNotificationReceiving(NotificationRequest request)
68+
//{
69+
// request.Title = $"{request.Title} Modified";
7070

71-
return Task.FromResult(new NotificationEventReceivingArgs
72-
{
73-
Handled = false,
74-
Request = request
75-
});
76-
}
71+
// return Task.FromResult(new NotificationEventReceivingArgs
72+
// {
73+
// Handled = false,
74+
// Request = request
75+
// });
76+
//}
7777

7878
private void ButtonCancel_Clicked(object sender, EventArgs e)
7979
{

Sample/Direct/LocalNotification.Sample.Android/LocalNotification.Sample.Android.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
1717
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
1818
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
19-
<TargetFrameworkVersion>v12.0</TargetFrameworkVersion>
19+
<TargetFrameworkVersion>v13.0</TargetFrameworkVersion>
2020
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
2121
<UseShortFileNames>True</UseShortFileNames>
2222
<IntermediateOutputPath>C:\temp\LocalNotification</IntermediateOutputPath>

Sample/Direct/LocalNotification.Sample.Android/MainActivity.cs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ protected override void OnCreate(Bundle savedInstanceState)
1616
base.OnCreate(savedInstanceState);
1717
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
1818

19+
LocalNotificationCenter.MainActivity = this;
20+
1921
// Must create a Notification Channel when API >= 26
2022
// you can created multiple Notification Channels with different names.
2123
// you can created multiple Notification Channel Groups with different names.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="10.0" package="com.localnotification.sample" android:installLocation="auto">
33
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="31" />
4+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
5+
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
6+
<uses-permission android:name="android.permission.VIBRATE" />
7+
<uses-permission android:name="android.permission.WAKE_LOCK" />
48
<application android:label="Local Notification Sample" android:icon="@drawable/Icon" android:theme="@style/MainTheme"></application>
59
</manifest>

Sample/Direct/LocalNotification.Sample/MainPage.xaml.cs

+5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ private async void Button_Clicked(object sender, EventArgs e)
135135

136136
try
137137
{
138+
if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
139+
{
140+
await LocalNotificationCenter.Current.RequestNotificationPermission();
141+
}
142+
138143
var ff = await LocalNotificationCenter.Current.Show(request);
139144
}
140145
catch (Exception exception)

Source/Plugin.LocalNotification/Platforms/Android/LocalNotificationCenter.cs

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using Android.App;
1+
using Android;
2+
using Android.App;
23
using Android.Content;
4+
using Android.Content.PM;
35
using Android.Media;
46
using Android.OS;
7+
using AndroidX.Core.App;
8+
using AndroidX.Core.Content;
59
using Microsoft.Extensions.Logging;
610
#if ANDROID
711
using Microsoft.Maui.ApplicationModel;
@@ -10,6 +14,7 @@
1014
using Plugin.LocalNotification.EventArgs;
1115
using Plugin.LocalNotification.Platforms;
1216
using System;
17+
using System.Diagnostics;
1318
using System.IO;
1419
using System.Runtime.CompilerServices;
1520
using System.Threading.Tasks;
@@ -19,19 +24,18 @@ namespace Plugin.LocalNotification
1924
public partial class LocalNotificationCenter
2025
{
2126
/// <summary>
22-
/// Not available for Xamarin.Forms please use Xamarin.Essentials.Permissions.RequestAsync
27+
///
2328
/// </summary>
2429
/// <returns></returns>
2530
public static async Task<bool> RequestNotificationPermissionAsync(NotificationPermission permission = null)
2631
{
32+
permission ??= new NotificationPermission();
2733
#if ANDROID
2834
if (!OperatingSystem.IsAndroidVersionAtLeast(33))
2935
{
3036
return false;
3137
}
3238

33-
permission ??= new NotificationPermission();
34-
3539
if (!permission.AskPermission)
3640
{
3741
return false;
@@ -40,11 +44,29 @@ public static async Task<bool> RequestNotificationPermissionAsync(NotificationPe
4044
var status = await Permissions.RequestAsync<NotificationPerms>();
4145
return status == PermissionStatus.Granted;
4246
#else
43-
var result = await Task.FromResult(true);
44-
return result;
47+
if (!permission.AskPermission)
48+
{
49+
return await Task.FromResult(false);
50+
}
51+
52+
if ((int)Build.VERSION.SdkInt >= 33)
53+
{
54+
var permissionName = "android.permission.POST_NOTIFICATIONS";
55+
56+
if (Application.Context.CheckSelfPermission(permissionName) != Permission.Granted)
57+
{
58+
ActivityCompat.RequestPermissions(MainActivity, new[] { permissionName }, 0);
59+
}
60+
}
61+
return await Task.FromResult(true);
4562
#endif
4663
}
4764

65+
/// <summary>
66+
///
67+
/// </summary>
68+
public static Android.App.Activity MainActivity { get; set; }
69+
4870
/// <summary>
4971
/// Notify Local Notification Tapped.
5072
/// </summary>

Source/Plugin.LocalNotification/Plugin.LocalNotification.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Description>The local notification plugin provides a way to show local notifications from Xamarin and MAUI apps.</Description>
2121
<PackageIcon>icon.png</PackageIcon>
2222
<Copyright>Copyright © Elvin (Tharindu) Thudugala</Copyright>
23-
<Version>10.1.7</Version>
23+
<Version>10.1.8</Version>
2424
<PackageReleaseNotes>Check: https://github.com/thudugala/Plugin.LocalNotification/releases </PackageReleaseNotes>
2525

2626
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -71,7 +71,7 @@
7171
<Compile Include="**/Platforms/Android/**/*.cs" />
7272
<PackageReference Include="Xamarin.Build.Download" Version="0.11.4" PrivateAssets="All"/>
7373
<PackageReference Include="Xamarin.AndroidX.Core" Version="1.9.0.1" />
74-
<PackageReference Include="Xamarin.GooglePlayServices.Location" Version="120.0.0.1" />
74+
<PackageReference Include="Xamarin.GooglePlayServices.Location" Version="120.0.0.0" />
7575
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
7676
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
7777
</ItemGroup>

0 commit comments

Comments
 (0)