Skip to content

Commit

Permalink
Fixed permission issue & Bump version from '2.0.5.2' to '2.0.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaijobu committed Mar 21, 2024
1 parent da3617f commit 8af5f59
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<TargetFrameworkVersion>v12.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v13.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="e_intidata.localnotificationssample">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:label="LocalNotificationsSample.Android" android:theme="@style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--https://stackoverflow.com/questions/71031091/android-12-using-schedule-exact-alarm-permission-to-get-show-data-at-specific-t-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Sample/Xamarin/LocalNotificationsSample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public App()
{
System.Diagnostics.Debug.WriteLine("Current Token : " + e.Token);
};

Device.BeginInvokeOnMainThread(async () =>
{
if (await NotificationCenter.Current.IsNotificationsEnabled() == false)
{
await NotificationCenter.Current.RequestNotificationPermission();
}
});

}

protected async void OnNotificationTapped(NotificationEventArgs e)
Expand Down
9 changes: 9 additions & 0 deletions Source/LocalNotifications/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,14 @@ public interface INotificationService
void Schedule(int notificationId, string title, string description, DateTime dateTime, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);

Task<string> GetTokenAsync();

/// <summary>
///
/// </summary>
/// <param name="permission"></param>
/// <returns></returns>
Task<bool> RequestNotificationPermission(NotificationPermission permission = null);

Task<bool> IsNotificationsEnabled();
}
}
5 changes: 3 additions & 2 deletions Source/LocalNotifications/LocalNotifications.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/3.0.44">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;MonoAndroid10.0;Xamarin.iOS10;net6.0-android;net6.0-ios;</TargetFrameworks> <!---->
<TargetFrameworks>netstandard2.0;MonoAndroid13.0;Xamarin.iOS10;net8.0;net8.0-android;net8.0-ios;</TargetFrameworks> <!---->
<SingleProject>true</SingleProject>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
Expand All @@ -20,7 +20,7 @@
<Description>A cross platform plugin for displaying local notifications.</Description>
<PackageReleaseNotes>Check: https://github.com/xDaijobu/LocalNotifications/releases </PackageReleaseNotes>
<Copyright>Copyright © Cristover Wurangian</Copyright>
<Version>2.0.5.2</Version>
<Version>2.0.5.3</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<AssemblyFileVersion>$(Version)</AssemblyFileVersion>
<PackageVersion>$(Version)$(VersionSuffix)</PackageVersion>
Expand Down Expand Up @@ -88,5 +88,6 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.4" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions Source/LocalNotifications/NotificationPermission.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LocalNotifications
{
public class NotificationPermission
{
public bool AskPermission { get; set; } = true;
}
}
4 changes: 4 additions & 0 deletions Source/LocalNotifications/Platform/Droid/ManifestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]

#if __ANDROID_33__
[assembly: UsesPermission(Manifest.Permission.PostNotifications)]
#endif

#if ANDROID31_0_OR_GREATER
[assembly: UsesPermission(Manifest.Permission.ScheduleExactAlarm)]
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Android;
using Android.OS;
using System;
using System.Collections.Generic;
using Xamarin.Essentials;

namespace LocalNotifications.Platform.Droid
{
public partial class NotificationPermissionAndroid : Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions
{
get
{
var result = new List<(string androidPermission, bool isRuntime)>();
if ((int)Build.VERSION.SdkInt >= 33)
{
result.Add((Manifest.Permission.PostNotifications, true));
}
return result.ToArray();
}
}
}
}
31 changes: 31 additions & 0 deletions Source/LocalNotifications/Platform/Droid/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using AndroidX.Core.App;
using Firebase.Messaging;
using Android.Gms.Extensions;
using Xamarin.Essentials;

namespace LocalNotifications.Platform.Droid
{
Expand Down Expand Up @@ -654,5 +655,35 @@ private void CreateNotificationChannelWithoutVibration(Context context)
EnableVibration = false,
});
}

public Task<bool> IsNotificationsEnabled()
{
return _notificationManager is null
? Task.FromResult(false)
: !((int)Build.VERSION.SdkInt >= 24)
? Task.FromResult(true)
: Task.FromResult(_notificationManager.AreNotificationsEnabled());
}


public async Task<bool> RequestNotificationPermission(NotificationPermission permission = null)
{
permission ??= new NotificationPermission();

if ((int)Build.VERSION.SdkInt < 33)
{
return false;
}

if (!permission.AskPermission)
{
return false;
}

var status = await Permissions.RequestAsync<NotificationPermissionAndroid>();
return status == PermissionStatus.Granted;
}


}
}
43 changes: 42 additions & 1 deletion Source/LocalNotifications/Platform/iOS/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using CoreLocation;
using Firebase.CloudMessaging;
using Firebase.Core;
using Foundation;
Expand Down Expand Up @@ -396,7 +397,47 @@ public List<NotificationRequest> GetPendingNotificationRequests()
return scheduledNotifications;
}

#region Others
public async Task<bool> IsNotificationsEnabled()
{
var settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync().ConfigureAwait(false);
return settings.AlertSetting == UNNotificationSetting.Enabled;
}

public async Task<bool> RequestNotificationPermission(NotificationPermission? permission = null)
{
try
{
permission ??= new NotificationPermission();

if (!permission.AskPermission)
{
return false;
}

var allowed = await IsNotificationsEnabled();
if (allowed)
{
return true;
}

// Ask the user for permission to show notifications on iOS 10.0+
var (alertsAllowed, error) = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(UNAuthorizationOptions.Sound).ConfigureAwait(false);

if (error != null)
{
Console.WriteLine(error);
}

return alertsAllowed;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
}

#region Others
private static IDictionary<string, object> GetParameters(NSDictionary dictionary)
{
var parameters = new Dictionary<string, object>();
Expand Down

0 comments on commit 8af5f59

Please sign in to comment.