Xamarin Forms Local Notifications

Today we are goign to use Xam.Plugins.Notifier for local notifications.

Later in other Hands On Lab I'm going to make an example for remote push notifications.

This local notification plugin provides a simple, cross-platform way to show local notifications from within Xamarin and UWP apps.

Let's open our proyect ShellDemo, if you don't have the Solution download it from here.

Open Manage NuGet packages for Solution and install Xam.Plugins.Notifier Nuget.

Go to the ShellDemo.Android properties and change the target framework to 9.0.

Close the properties and open AppShell.xaml inside the portable proyect, then go to line number 52 and replace "local:AboutPage" for "local:LocalNotificationPage".

Then inside the Views folder add a new ContentPage with the name: LocalNotificationPage and in the LocalNotificationPage.xaml file, copy this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
                    x:Class="ShellDemo.Views.LocalNotificationPage"
                    xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
                    <ContentPage.Content>
                    <StackLayout>
                    <Button
                    HorizontalOptions="CenterAndExpand"
                    Text="Click Here to show the notification"
                    VerticalOptions="CenterAndExpand" Clicked="Button_Clicked"/>
                    </StackLayout>
                    </ContentPage.Content>
</ContentPage>

After that go to Portable Proyect > Views, open LocalNotificationPage.xaml.cs file and copy this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using Plugin.LocalNotifications;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ShellDemo.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
                    public partial class LocalNotificationPage : ContentPage
                    {
                    public LocalNotificationPage()
                    {
                    InitializeComponent();
                    }

                    private void Button_Clicked(object sender, EventArgs e)
                    {
                    CrossLocalNotifications.Current.Show("Title", "Body");
                    }
                    }
}

Finally test the application, when you hit the button the notification will apeear.

Related Links

Xam.Plugins.Notifier Plugin

Xam.Plugins.Notifier Documentation Here

Completed Solution Shell Demo

Results


Share this post