Telerik blogs
XamarinT2 Dark_1200x303

Get acquainted with local notifications, the alerts sent by applications installed on our mobile device without needing a back-end server to push them.

Has it ever happened to you that it was one of your best friend’s birthdays but you forgot because nobody and nothing reminded you? Yes, me too, πŸ™ˆ and that can be annoying.

But what if you could write down those important reminders in your cell phone and it could pop up a reminder on the right day and time, preventing you from getting into trouble because you forgot that important date! 😎 As developers, we call this: Local Notifications and in this article we will be learning how implement them!

First of All … What Important Things Do I Need to Know? πŸ€”

βœ” You have to add from NuGet Package: Xam.Notifier.Plugin.

Xam.Notifier.Plugin Cross platform plugin to show local notifications

βœ” Platform Support

Platform Version
Xamarin.iOS 7.0+
Xamarin.Android 3.0+ (API 11+)
Xamarin.Mac 10.7+
Windows (UWP) 10.0+

Let’s Start!

βœ” What Are Local Notifications?

Local notifications are alerts sent by applications installed on our mobile device without the need of a back-end server to push them (hence the word local) and are often used for features such as: calendar events, reminders and location-based triggers.

Let’s Learn How They’re Used

CrossLocalNotifications is a class that allows us to show or cancel notifications from our device. To use this class we have to call the Plugin.LocalNotifications library and it has the following abstracted methods:

βž– CrossLocalNotifications.Current.Show: It allow us to show a notification in our device and receives the following parameters:

Name Description Data type Is Mandatory?
Title It’s the title shown in the notification. string βœ”
Body It’s the message body that will have the notification. string βœ”
ID It’s the ID with which we can identify the notification for future operations. (For example: If you want to cancel them). string ❌
Time to notify It’s the time when the notification will be released. DateTime ❌

Applying them in code, we would have a result like this:

CrossLocalNotifications.Current.Show("Local notifications", "Howdy! This is a local notification test.", 0, DateTime.Now.AddSeconds(5));

βž– CrossLocalNotifications.Current.Cancel: It allows us to cancel a notification sent. Receives the notification ID that we added when we created a notification.

CrossLocalNotifications.Current.Cancel(0);

Once a notification is implemented, you should see something like this:

Notification Structure: Icon, Title, Body.

πŸ”§ Platform settings

πŸ“˜ Windows and Windows Phone 8.1

βž– In the .appmanifest file, set the “Toast capable” property to “Yes”. This action allows you to enable notifications.

πŸ“— Android

βž– You don’t need some additional setting. Just take into account that when the phone is rebooted, any pending notifications are not sent. If you want to re-send them, you must save them out to settings and re-send.

πŸ“’ iOS

βž– On iOS 8.0+
To show a local notification on iOS 8.0, you need to get permission. To do so, include the following code in the FinishedLaunching() method of AppDelegate:

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
        // Ask the user for permission to get notifications on iOS 10.0+
        UNUserNotificationCenter.Current.RequestAuthorization(
        UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
        (approved, error) => { });
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
        // Ask the user for permission to get notifications on iOS 8.0+
        var settings = UIUserNotificationSettings.GetSettingsForTypes(
        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
        new NSSet());
        UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}

βž– On iOS 10.0+

On iOS 10.0+, in order to specify how notifications are handled when the app is active, you must create a delegate class that subclasses UNUserNotificationCenterDelegate and assign it to the UNUserNotificationCenter.

using UserNotifications;

namespace LocalNotifications.iOS.Notifications
{
   public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
   {
      public UserNotificationCenterDelegate()
      {
      }
   }
}

πŸ”† Notification Icon on Android

If you want to add an icon to your notification, just add the following line of code to your MainActivity inside the OnCreate method:

LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.YOUR_ICON_NAME;

That’s all for today! I hope this article is useful for you.

Thanks for reading! πŸ’š

References: https://github.com/edsnider/localnotificationsplugin


LeomarisReyes
About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! πŸ’šπŸ’• You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts

Comments

Comments are disabled in preview mode.