Clear all Notifications from Notification Settings

Hi all,
I originally asked this question on the Xamarin.iOS group but I think the folks around here are the ones who can help:
I've integrated push notifications with FCM in my Xamarin iOS and Android projects. My push notifications are coming to my app correctly, however when a user comes to a specific page of my application I'd like to clear all notifications from the notification center. I created a DependencyService to clear notifications and call it in the OnAppearing() method of that specific page. In Android, it works beautifully. However in iOS I can't seem to get it to work. I tried calling this: UIApplication.SharedApplication.CancelAllLocalNotifications();
but that seems to only be for local notifications. After a bit of research from the apple documentation this seems possible, https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649502-removealldeliverednotifications
however I don't know how I would implement it in my code.
Any help would be greatly appreciated!
Thanks!

Answers

  • RendyDelRosarioRendyDelRosario DOUniversity ✭✭✭
     if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
     {
        UNUserNotificationCenter.Current.RemoveAllPendingNotificationRequests(); // To remove all pending notifications which are not delivered yet but scheduled.
        UNUserNotificationCenter.Current.RemoveAllDeliveredNotifications(); // To remove all delivered notifications
    } else {
       UIApplication.SharedApplication.CancelAllLocalNotifications();
    }
    
  • VenkataSwamyVenkataSwamy INMember ✭✭✭

    Hi @RachelGreenberger

    I did a small sample on how to clear push notifications, follow below code

    PCL:

    PushCancelPage.xaml

      <?xml version="1.0" encoding="utf-8"?>  
      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:PushCancel" 
       x:Class="PushCancel.PushCancelPage">  
      <Label Text="Push Notification Cancel Demo" VerticalOptions="Center" HorizontalOptions="Center" />  
     </ContentPage>  
    

    PushCancelPage.cs

     using System;  
     using Xamarin.Forms;  
    
     namespace PushCancel  
      {  
         public partial class PushCancelPage : ContentPage  
          {  
             public PushCancelPage()  
              {  
                  InitializeComponent();  
                  DependencyService.Get<IPushCancel>().CancelPush(Convert.ToInt32("Pass Your NotificationID"));  
             }  
         }  
      }  
    

    IPushCancel.cs:

        using System;  
        namespace PushCancel  
         {  
             public interface IPushCancel  
              {  
                 void CancelPush(int id);  
             }  
      }  
    

    Xamarin.Android:

    IPushCancelService_Android.cs

    using Xamarin.Forms;  
    using Android.Provider;  
    using PushCancel.Droid;  
    using Android.Telephony;  
    using System;  
    using Java.Util;  
    using PushCancel;  
    using Android.App;  
    using Android.Support.V4.App;  
    using Android.Content;  
    
    [assembly: Dependency(typeof(IPushCancelService_Android))]  
    namespace PushCancel.Droid  
    {  
       public class IPushCancelService_Android : IPushCancel  
       {  
    
             public void CancelPush(int id)  
             {  
                var intent = CreateIntent(id);  
               var pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, id, intent,          PendingIntentFlags.CancelCurrent);  
             var alarmManager = GetAlarmManager();  
             alarmManager.Cancel(pendingIntent);  
              var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);  
             notificationManager.CancelAll();  
    
          }  
           private AlarmManager GetAlarmManager()  
              {  
                  var alarmManager = Android.App.Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;  
                 return alarmManager;  
             }  
    
          private Intent CreateIntent(int id)  
              {  
                  return new Intent(Android.App.Application.Context, typeof(AppGcmListenerService /*"Here we have to put GCM Listener class"*/))  
                    .SetAction("LocalNotifierIntent" + id);  
                }  
         }  
        }  
    

    Xamarin.iOS:

    IPushCancelService_iOS.cs

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;    
    using Foundation;  
    using UIKit;  
    using Xamarin.Forms;  
    using PushCancel.iOS;  
    
    [assembly: Dependency(typeof(IPushCancelService_iOS))]  
    namespace PushCancel.iOS  
       {  
         public class IPushCancelService_iOS : IPushCancel  
           {  
    
                  private const string NotificationKey = "LocalNotificationKey";  
    
               public void CancelPush(int id)  
                 {  
                  UIApplication.SharedApplication.CancelAllLocalNotifications();  
                  var notifications = UIApplication.SharedApplication.ScheduledLocalNotifications;  
                 var notification = notifications.Where(n => n.UserInfo.ContainsKey(NSObject.FromObject(NotificationKey)))  
                .FirstOrDefault(n => n.UserInfo[NotificationKey].Equals(NSObject.FromObject(id)));  
                    UIApplication.SharedApplication.CancelAllLocalNotifications();  
                 if (notification != null)  
                    {  
                       UIApplication.SharedApplication.CancelLocalNotification(notification);  
                         UIApplication.SharedApplication.CancelAllLocalNotifications();  
                 }  
                 }  
          }  
        }  
    

    It's working for me

  • SreeeeSreeee INMember ✭✭✭✭✭

    @VenkataSwamy I tried this solution, but it's not working for me. Also, I go through your blog.

    IOS

    No error or exception is showing in ios, but code execution is not going inside of the following `if.

    if (notification != null)
                {
                    UIApplication.SharedApplication.CancelLocalNotification(notification);
                    UIApplication.SharedApplication.CancelAllLocalNotifications();
                }
    

    Android

    Getting TargetInvocationException when sending notification id from Mainactitivity.

    Exception Details:

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Input string was not in a correct format.

    I started a new thread for this feature, can you please have a look?

Sign In or Register to comment.