Remove Multiple FCM Messages- Xamarin Forms - iOS

I have implemented FCM in Xamarin Forms and is working as expected. But I want to remove all the notifications from the notification tray when a new push is received.

I have able to do the same with the Tag key in Android, how to achieve the same on iOS.

Can someone help me with this?

Answers

  • Mr.PearceMr.Pearce DEMember ✭✭

    on ios 10 you can use this to delete all messages except the new one

    messageId = your new message id (its the gcm.message_id value)
    private string _gcmMessageIdKey = "gcm.message_id";

    UNUserNotificationCenter.Current.GetDeliveredNotifications(notifications =>
       {
           var pushs = notifications.Where(x => x.Request.Content.UserInfo.ContainsKey(new NSString(_gcmMessageIdKey))).ToList();
           if (pushs.Count > 0)
           {
               foreach (var notification in pushs)
               {
                   var foundId = notification.Request.Content.UserInfo[_gcmMessageIdKey] as NSString;
                   if (foundId != messageId)
                   {
                       var messageIds = new[] { notification.Request.Identifier };
                       UNUserNotificationCenter.Current.RemoveDeliveredNotifications(messageIds);
                   }
               }
           }
           else
           {
               var messageIds = new[] { messageId };
               UNUserNotificationCenter.Current.RemoveDeliveredNotifications(messageIds);
           }
       });
    
  • ASHISHKUMAR.8068ASHISHKUMAR.8068 USMember ✭✭

    @Mr.Pearce I am using FCM .Can you help me with the messageId in FCM

  • Mr.PearceMr.Pearce DEMember ✭✭
    edited July 2017

    @ASHISHKUMAR.8068 said:
    @Mr.Pearce I am using FCM .Can you help me with the messageId in FCM

    Sry for the misunderstanding but this IS for FCM although it is named "gcm.message_id"

    (its from my fcm source)

  • VenkataSwamyVenkataSwamy INMember ✭✭✭

    Hi @ASHISHKUMAR.8068,

    For this functionality, we have to write the DependencyService

    https://forums.xamarin.com/discussion/comment/287599#Comment_287599

    Call that DependencyService in DidReceiveRemoteNotification

  • Mr.PearceMr.Pearce DEMember ✭✭

    @BalaRaju said:
    Hi @ASHISHKUMAR.8068,

    For this functionality, we have to write the DependencyService

    https://forums.xamarin.com/discussion/comment/287599#Comment_287599

    Call that DependencyService in DidReceiveRemoteNotification

    You don't have to call or make a DependencyService if you are on OS level like DidReceiveRemoteNotification.
    You only need DependencyServices if you want to use this from a pcl/shared/Forms project.

Sign In or Register to comment.