Toast Notifications For Xamarin Forms

In this article we are going to implement xamarin forms toast notification using a plugin. All step by step guide, code and output is also given in this article.

Plugin to install:

Toasts.Forms.Plugin

You can access plugin version details and other document by clicking on the link above.

Firstly you have to Install this plugin in all projects.

 

After installing plugin in all platforms. You need to initialize it on all platforms just like this.

In UWP:

In UWP on (MainPage.cs) initialize toast notification after initializing component add the code to initialize.

DependencyService.Register<ToastNotification>();
 ToastNotification.Init();

 

In Android:

In Android on (MainActivity.cs)  after Xamarin.Forms.Init() add the following code:

DependencyService.Register<ToastNotification>();
 ToastNotification.Init(this);

 

In iOS:

In iOS on (AppDelegate.cs) after Xamarin.Forms.Init(); add the following code:

DependencyService.Register<ToastNotification>();
   ToastNotification.Init();

In iOS you also need to request a permission for notifications. You need to add the following code for this. after ToastNotification.Init();

 UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) =>
 {
 // Do something if needed
 });

 

Code:

 

 var options = new NotificationOptions()
 {
 Title = "Hello",
 Description = "Welcome To Xamarin Web",
 IsClickable = false, // Set to true if you want the result Clicked to come back (if the user clicks it)
 
 };
 var notification = DependencyService.Get<IToastNotificator>();
 var result = await notification.Notify(options);

 

Now by this code you can easily use notifications in project.

You can also set image on notification by this adding the code below in notificationOptions:

 

WindowsOptions = new WindowsOptions() { LogoUri = "yourimage.png" }

Output:

 

 

By clicking on show toast button you can see notifications.

 

XamarinWeb Founder | MVP – C-SharpCorner | Xamarin Forms Developer

Leave a Reply

Your email address will not be published. Required fields are marked *