Skip to content

Commit

Permalink
Fixed notification cancel issue
Browse files Browse the repository at this point in the history
Fixed AssemblyMetadata IsTrimmable is set to True now
UsesPermission must be set at application level
  • Loading branch information
thudugala committed May 20, 2023
1 parent d8b6f4b commit ce6fb5c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Sample/Direct Maui/LocalNotification.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
HorizontalOptions="Center"
Text="Clear Text"
VerticalOptions="Center" />
<Button
Clicked="ButtonCancel_Clicked"
HorizontalOptions="Center"
Text="Cancel Local Notification"
VerticalOptions="Center" />

<!-- Place new controls here -->
<StackLayout VerticalOptions="CenterAndExpand">
Expand Down
11 changes: 9 additions & 2 deletions Sample/Direct Maui/LocalNotification.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public MainPage(INotificationService notificationService)
NotifyDatePicker.MinimumDate = DateTime.Today;
NotifyTimePicker.Time = DateTime.Now.TimeOfDay.Add(TimeSpan.FromSeconds(10));

//ScheduleNotificationGroup();
//ScheduleNotification("first", 10);
ScheduleNotificationGroup();
ScheduleNotification("first", 10);
//ScheduleNotification("second", 20);

_cacheFilePath = FileSystem.Current.CacheDirectory + $"/testFile.txt";
Expand Down Expand Up @@ -75,6 +75,11 @@ private Task<NotificationEventReceivingArgs> OnNotificationReceiving(Notificatio
});
}

private void ButtonCancel_Clicked(object sender, EventArgs e)
{
_notificationService.CancelAll();
}

private async void Button_Clicked(object sender, EventArgs e)
{
var imageStream = GetType().Assembly.GetManifestResourceStream("LocalNotification.Sample.Resources.appicon.png");
Expand Down Expand Up @@ -280,6 +285,8 @@ private void ScheduleNotification(string title, double seconds)
Schedule =
{
NotifyTime = DateTime.Now.AddSeconds(seconds),
RepeatType = NotificationRepeat.TimeInterval,
NotifyRepeatInterval = TimeSpan.FromSeconds(10),
}
};
_notificationService.Show(notification);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//Permissions for android

using Android;
using Android.App;

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

[assembly: UsesPermission(Manifest.Permission.ScheduleExactAlarm)]
[assembly: UsesPermission(Manifest.Permission.PostNotifications)]
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
using Microsoft.Maui.Controls.PlatformConfiguration;
#endif

[assembly: UsesPermission(Manifest.Permission.WakeLock)]
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
[assembly: UsesPermission(Manifest.Permission.Vibrate)]
//[assembly: UsesPermission(Manifest.Permission.WakeLock)]
//[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
//[assembly: UsesPermission(Manifest.Permission.Vibrate)]

#if ANDROID
[assembly: UsesPermission(Manifest.Permission.ScheduleExactAlarm)]
[assembly: System.Reflection.AssemblyMetadata("IsTrimmable", "False")]
//[assembly: UsesPermission(Manifest.Permission.ScheduleExactAlarm)]
[assembly: System.Reflection.AssemblyMetadata("IsTrimmable", "True")]

#if NET7_0_OR_GREATER
[assembly: UsesPermission(Manifest.Permission.PostNotifications)]
//[assembly: UsesPermission(Manifest.Permission.PostNotifications)]
#else
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
//[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
#endif

#elif MONOANDROID
[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
//[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]
//[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
[assembly: LinkerSafe]
#endif

Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ await MyGeofencingClient
/// <returns></returns>
protected virtual PendingIntent CreateGeofenceIntent(int notificationId, string serializedRequest)
{
var pendingIntent = CreateActionIntent(serializedRequest, new NotificationAction(notificationId)
var pendingIntent = CreateActionIntent(notificationId, serializedRequest, new NotificationAction(0)
{
Android =
{
Expand Down Expand Up @@ -367,7 +367,7 @@ internal virtual bool ShowLater(NotificationRequest request)
/// <returns></returns>
protected virtual PendingIntent CreateAlarmIntent(int notificationId, string serializedRequest)
{
var pendingIntent = CreateActionIntent(serializedRequest, new NotificationAction(notificationId)
var pendingIntent = CreateActionIntent(notificationId, serializedRequest, new NotificationAction(0)
{
Android =
{
Expand Down Expand Up @@ -538,7 +538,7 @@ internal virtual async Task<bool> ShowNow(NotificationRequest request)

var serializedRequest = LocalNotificationCenter.GetRequestSerialize(request);

var contentIntent = CreateActionIntent(serializedRequest, new NotificationAction(NotificationActionEventArgs.TapActionId)
var contentIntent = CreateActionIntent(request.NotificationId, serializedRequest, new NotificationAction(NotificationActionEventArgs.TapActionId)
{
Android =
{
Expand All @@ -547,7 +547,7 @@ internal virtual async Task<bool> ShowNow(NotificationRequest request)
}
}, typeof(NotificationActionReceiver));

var deleteIntent = CreateActionIntent(serializedRequest, new NotificationAction(NotificationActionEventArgs.DismissedActionId)
var deleteIntent = CreateActionIntent(request.NotificationId, serializedRequest, new NotificationAction(NotificationActionEventArgs.DismissedActionId)
{
Android =
{
Expand Down Expand Up @@ -689,7 +689,7 @@ protected virtual async Task<Bitmap> GetNativeImage(NotificationImage notificati
protected virtual NotificationCompat.Action CreateAction(NotificationRequest request, string serializedRequest,
NotificationAction action)
{
var pendingIntent = CreateActionIntent(serializedRequest, action, typeof(NotificationActionReceiver));
var pendingIntent = CreateActionIntent(request.NotificationId, serializedRequest, action, typeof(NotificationActionReceiver));
if (string.IsNullOrWhiteSpace(action.Android.IconName.ResourceName))
{
action.Android.IconName = request.Android.IconSmallName;
Expand All @@ -704,11 +704,12 @@ protected virtual NotificationCompat.Action CreateAction(NotificationRequest req
/// <summary>
///
/// </summary>
/// <param name="notificationId"></param>
/// <param name="serializedRequest"></param>
/// <param name="action"></param>
/// <param name="broadcastReceiverType"></param>
/// <returns></returns>
protected virtual PendingIntent CreateActionIntent(string serializedRequest, NotificationAction action, Type broadcastReceiverType)
protected virtual PendingIntent CreateActionIntent(int notificationId, string serializedRequest, NotificationAction action, Type broadcastReceiverType)
{
var notificationIntent = action.Android.LaunchAppWhenTapped
? (Application.Context.PackageManager?.GetLaunchIntentForPackage(Application.Context.PackageName ??
Expand All @@ -720,7 +721,9 @@ protected virtual PendingIntent CreateActionIntent(string serializedRequest, Not
.PutExtra(LocalNotificationCenter.ReturnRequestActionId, action.ActionId)
.PutExtra(LocalNotificationCenter.ReturnRequest, serializedRequest);

var requestCode = _random.Next();
//var requestCode = _random.Next();
// Cannot be random, then you cannot cancel it.
var requestCode = notificationId + action.ActionId;

var pendingIntent = action.Android.LaunchAppWhenTapped
? PendingIntent.GetActivity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#if XAMARINIOS
[assembly: LinkerSafe]
#elif IOS
[assembly: System.Reflection.AssemblyMetadata("IsTrimmable", "False")]
[assembly: System.Reflection.AssemblyMetadata("IsTrimmable", "True")]
#endif

Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public async Task<bool> Show(NotificationRequest request)
return false;
}
#elif IOS
if (!OperatingSystem.IsIOS())
{
return false;
}
if (!OperatingSystem.IsIOSVersionAtLeast(10))
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Description>The local notification plugin provides a way to show local notifications from Xamarin and MAUI apps.</Description>
<PackageIcon>icon.png</PackageIcon>
<Copyright>Copyright © Elvin (Tharindu) Thudugala</Copyright>
<Version>10.1.4</Version>
<Version>10.1.5</Version>
<PackageReleaseNotes>Check: https://github.com/thudugala/Plugin.LocalNotification/releases </PackageReleaseNotes>

<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down

0 comments on commit ce6fb5c

Please sign in to comment.