Answer the question
In order to leave comments, you need to log in
How can I cancel local notifications?
Local notifications are generated. It is necessary to repeat notifications if the user ignores them. When the user entered the application and performed the necessary actions, it is necessary to reset the notifications.
Notifications should come even when the app is turned off.
Now I have a problem that incoming notifications do not turn off.
Please tell me what am I doing wrong?
The code I use to solve the problem:
public static Task Send(LocalNotification notification, bool testParam = false)
{
if (!isSchedule)
{
var alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
var serializedNotification = SerializeNotification(notification);
var triggerTime = NotifyTimeInMilliseconds(notification.NotifyTime);
var pending = GetPendingIntent(notification.Id, serializedNotification);
alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, Convert.ToInt64(triggerTime), 5000, pending);
}
else
{
try
{
var launchIntent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(Android.App.Application.Context.PackageName);
launchIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var builder = new NotificationCompat.Builder(Android.App.Application.Context)
.SetAutoCancel(true)
.SetContentTitle(notification.Title)
.SetContentText(notification.Text)
.SetSmallIcon(Android.App.Application.Context.ApplicationInfo.Icon)
.SetContentIntent(Android.App.TaskStackBuilder.Create(Android.App.Application.Context).AddNextIntent(launchIntent).GetPendingIntent(notification.Id, PendingIntentFlags.UpdateCurrent))
.SetVibrate(new long[] { 500, 500 })
.SetSound(Android.Net.Uri.Parse("android.resource://" + Android.App.Application.Context.PackageName + "/" + Resource.Raw.local_notifications));
var not = builder.Build();
NotificationManagerCompat.From(Android.App.Application.Context).Notify(notification.Id, not);
}
catch (Exception exp)
{
var errorMsg = exp.Message;
}
}
}
public void Cancel(int id)
{
var intent = CreateIntent(id);
var pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, id, intent, PendingIntentFlags.CancelCurrent);
pendingIntent.Cancel();
var alarmManager = GetAlarmManager();
alarmManager.Cancel(pendingIntent);
var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
notificationManager.CancelAll();
}
private static PendingIntent GetPendingIntent(int id, string nottification)
{
var intent = new Intent(Android.App.Application.Context, typeof(ScheduledAlarmHandler)).PutExtra(NotificationString, nottification);
var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, id, intent, PendingIntentFlags.CancelCurrent);
return pending;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question