К основному контенту

Local notifications in Android with Xamarin

I've been developing an Android mobile app for my pet project. I chose Xamarin framework as a tool just because I'm familiar with C# (even probably writing a native app is much better idea). Since I'm not a mobile developer I've been facing issues from time to time. In particular I realized that enabling local notifications (e.g. reminders) is not that much clear as I would love to.
So, this post will go through few steps showing how to set a local notification (or reminder).

The info is up-to-date in September, 2020.

NB: the post is relevant for Android 8.0 (Oreo) and higher since a feature of notification channels was introduced for those versions (see details here). The code was tested on a phone with Android 9.0 Pie.

Let's start. To demonstrate the process I built a simple app with just two buttons. See code in my Github repository. One button is to set a notification at a selected day and time. Another button will cancel the already set notification. For the sake of simplicity to select day and time I put to the app interface just the text entry fields (no appearing calendar or watch or something like that).

  



So, the flow is as following:
1. Add to your Android solution a NuGet package Xamarin.Android.Support.v4

2. In the main solution add a new class with the "Interface" type (see the code in InterfaceReminder.cs). This is to call a native service in your cross-platform code. Within that interface class you need declare the methods that will be called from app pages in Xamarin.Forms. In the example app there are two buttons. One calls the method "SetNotification" and another calls the method "CancelNotification".

3. Add a new class to Android project (in solution it's called AlarmReceiver.cs). This class is inherited from BroadcastReceiver and here is the code what app should do when called by Android OS. Don't forget to allow Xamarin.Android.Support.v4 namespace.

4. Add another new class to Android project (called AndroidReminderService.cs on Github). Enable Xamarin.Forms namespace and comment System.Runtime.CompilerService namespace. In this class the methods SetNotification and CancelNotification are codded. Remember that a notification is created with an unique ID. Then by the ID the notification can be cancelled or updated. In the example zero value is used for the simplicity.

5. In Android project in resource/drawable folder put an icon that will be used in notification. Set the correct name of the icon in the BroadcastReceiver class (AlarmReceiver.cs in the example)

6. That's it! Now if you call the SetNotification method with desired DateTime value as parameter Android will push the notification at the specified time with specified picture and text. If user taps on the notification the app will be opened. By calling CancelNotification method with a desired ID as parameter the previously set notification with that ID will be canceled.  You also may change the priority, visibility and other settings of the notifications. See more details in official documentation

See code here: https://github.com/quasi-researcher/LocalAndroidNotification

Комментарии