Moving to Notification Channels
- PDF for offline use
- Sample Code:
- Related Articles:
Let us know how you feel about this
last updated: 2017-12
This recipe demonstrates how to convert an existing Xamarin.Android app to use the notification channels feature introduced in Android Oreo.
In this recipe, an existing Xamarin.Android app is converted to use the new notification channels feature that was introduced in Android Oreo. The following steps explain how to convert the Local Notifications sample to use notification channels:
Download and open the Local Notifications sample in Visual Studio. Open the project properties and change the Target Framework to Android 8.0 (Oreo):
Open MainActivity.cs and add the following constant string definition to the
MainActivityclass. This constant is used as an ID string for a notification channel that will be added in a later step. This string must be unique within the package so that it is not confused with any other notification channels:
public const string CLICKS_CHANNEL = "com.xamarin.myapp.clicks";
Move the code that creates the notification manager so that it is called before
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); NotificationCompat.Builder builder = ...
After the line that creates
notificationManager, add the following code to create a notification channel.
CLICKS_CHANNELspecifies the channel ID and Button Click is the user-visible name of the channel. The channel importance is set to
Defaultand lock screen visibility is set to
Private(i.e., the notification will not show on the lock screen):
var chan = new NotificationChannel(CLICKS_CHANNEL, "Button Click", NotificationImportance.Default); chan.LockscreenVisibility = NotificationVisibility.Private;
Next, pass the notification channel to the Notification Manager:
Modify the call to the
NotificationCompat.Builderconstructor so that the ID of the channel is passed in as the second argument:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CLICKS_CHANNEL) .SetAutoCancel(true) .SetContentIntent(resultPendingIntent) ...
Build and run the modified app on an Android 8.0 phone or emulator. When you click the button, notifications are now posted locally via your "Button Click" notification channel:
From the Settings menu, tap Apps & Notifications > App Info > Notifications.Android > App Notifications. The Button Click notification channel is displayed under Categories:
Try adding a second notification channel by doing the following:
Add a second button with the text, ALERT!.
Write a click event handler for this second button.
Define another channel ID (such as
ALERT_CHANNEL), using a new unique text string (such as
Create a second
NotificationsChannelobject with this ID. Tive it the name Important Alerts and a notification importance of
High. Set the channel's
Publicso that this channel's notifications will appear on the lock screen.
Pass this new channel to
NotificationCompat.Builderand post a notification when the ALERT button is clicked.
Build it and try it out. This second channel will appear under app permission Categories as Important Alerts.
For more information about notification channels in Xamarin.Android, see Notification Channels.
This recipe provided step-by-step instructions for converting existing local notifications functionality of a Xamarin.Android app to the new notification channel feature introduced in Android 8.0 Oreo.