How to hide notification using NotificationBuilder(iOS) in Xamarin.forms?

developer007developer007 ✭✭Member ✭✭

Hi,

I use following way to show notification on page in Xamarin.Forms Application. I want hide this notification.

Can anybody please suggest how to hide this notification? Is there any method?

I show 3 notification as below.

Please suggest

MyProject.iOS

public class NotificationService : INotificationService
{
    private UIViewController RootViewController => UIApplication.SharedApplication.KeyWindow.RootViewController;
    private UIViewController PresentedViewController => UIApplication.SharedApplication.KeyWindow.RootViewController.PresentedViewController;

    public Task DisplayNotification(string message)
    {
        var notificationFrame = new CGRect(0, 64, RootViewController.View.Bounds.Width, 95);

        var notificationView = new NotificationBuilder().WithDescriptionText(message)
                                                        .WithFrameSize(notificationFrame)
                                                        .Create();

        RootViewController.View.AddSubview(notificationView);
        RootViewController.View.BringSubviewToFront(notificationView);

        return Task.Factory.StartNew(() => { });
    }

    public Task ShowUserNotFoundNotification(string message)
    {
        var notificationFrame = new CGRect(0, 64, RootViewController.View.Bounds.Width, 95);

        var view = new NotificationBuilder().WithColor(Colors.d8Red)
                                            .WithFrameSize(notificationFrame)
                                            .WithDescriptionText(message)
                                            .Create();

        PresentedViewController.View.AddSubview(view);
        PresentedViewController.View.BringSubviewToFront(view);

        return Task.Factory.StartNew(() => { });
    }

    public Task DisplayNotificationWithColor(string message, Color? backgroundColor)
    {
        UIApplication.SharedApplication.InvokeOnMainThread(() =>
        {
            var notificationFrame = new CGRect(0, 64, RootViewController.View.Bounds.Width, 95);

            var notificationView = new NotificationBuilder().WithDescriptionText(message)
                                                            .WithFrameSize(notificationFrame)
                                                            .WithColor(backgroundColor.Value)
                                                            .Create();

            UIApplication.SharedApplication.Windows.Last().AddSubview(notificationView);
            UIApplication.SharedApplication.Windows.Last().BringSubviewToFront(notificationView);
        });

        return Task.Factory.StartNew(() => { });
    }

    public void Hide()
    {
        //Not implememted
        throw new NotImplementedException();
    }
}
Sign In or Register to comment.