Cancel local notifications

Oleg_123Oleg_123 ✭✭USMember ✭✭
edited December 2017 in Xamarin.Android

Good afternoon.
I create local notifications that must be repeated if the user does not respond to them (1 minute interval)
When the user goes into the application, they need to be canceled.
Now my notifications are not canceled. What am I doing wrong?

Code:

` 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;
}`

Answers

  • Sonia_GSonia_G ✭✭ Member ✭✭

    Hi,

    it has been a while since this question has been asked but I have recently been facing the same issue and therefore, I will post how I have solved it because it can be of some help for someone.

    I have set all PendingIntentFlags to PendingIntentFlags.UpdateCurrent and all requestCode to 0 (the request code should be identical at all places and 0 was sufficient in my case).

    Here is my code for sending a notification once a week:

        int _notificationIconId { get; set; }
            readonly DateTime _jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    
            public void LocalNotification(string title, string body, int id, DateTime notifyTime)
            {
                long totalMilliSeconds = (long)(notifyTime.ToUniversalTime() - _jan1st1970).TotalMilliseconds;
    
                var intent = CreateIntent(id);
                var localNotification = new LocalNotification();
                localNotification.Title = title;
                localNotification.Body = body;
                localNotification.Id = id;
                localNotification.NotifyTime = notifyTime;
    
                if (_notificationIconId != 0)
                {
                    localNotification.IconId = _notificationIconId;
                }
                else
                {
                    localNotification.IconId = Resource.Mipmap.your_app_icon;
                }
    
                var serializedNotification = SerializeNotification(localNotification);
                intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);
    
                var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent);
                var alarmManager = GetAlarmManager();
                alarmManager.SetRepeating(AlarmType.RtcWakeup, totalMilliSeconds, AlarmManager.IntervalDay * 7, pendingIntent);
            }
    
            public void Cancel(int id)
            {
    
                var intent = CreateIntent(id);
                var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent);
                var alarmManager = GetAlarmManager();
                alarmManager.Cancel(pendingIntent);
                var notificationManager = NotificationManagerCompat.From(Application.Context);
                notificationManager.CancelAll();
                notificationManager.Cancel(id);
            }
    

    It is called with:
    DependencyService.Get<ILocalNotificationService>().LocalNotification("Title", "Notification Text", 0, dateTime);
    and canceled with:
    DependencyService.Get<ILocalNotificationService>().Cancel(0);

    Regards,
    Sonia

Sign In or Register to comment.