Notification Channels - StansAssets/com.stansassets.android-native GitHub Wiki

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.

You can use the AN_Build class API if you want to check current system android API level.

After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. Though you can still change a channel's name and description.

You should create a channel for each distinct type of notification you need to send. You can also create notification channels to reflect choices made by users of your app. For example, you can set up separate notification channels for each conversation group created by a user in a messaging app.

When you target Android 8.0 (API level 26), you must implement one or more notification channels. If your targetSdkVersion is set to 25 or lower, when your app runs on Android 8.0 (API level 26) or higher, it behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

Warning: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, Android Native Pro will create the channel default app channel automatically.

Create a notification channel

To create a notification channel, follow these steps:

  1. Construct an AN_NotificationChannel object with a unique channel ID, a user-visible name, and an importance level.
  2. Optionally, specify the description that the user sees in the system settings with SetDescription().
  3. Register the notification channel by passing it to CreateNotificationChannel().
using SA.Android.OS;
using SA.Android.App;
...
string channelId = "my_channel_id";
string name = "My Channel Name";
string description = "My Channel Description";
var importance = AN_NotificationManager.Importance.DEFAULT;

AN_NotificationChannel channel = new AN_NotificationChannel(channelId, name, importance);
channel.Description = description;

// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
AN_NotificationManager.CreateNotificationChannel(channel);

Creating an existing notification channel with its original values performs no operation, so it's safe to call this code when starting an app.

By default, all notifications posted to this channel use the visual and auditory behaviors defined by the importance level from the AN_NotificationManager class, such as AN_NotificationManager.Importance.DEFAULT and AN_NotificationManager.Importance.HIGH. (See below for more information about importance levels.)

Notification Channel Sound.

You may also provide a custom notification sound that will be used for all the notifications with this channel. See the notifications custom sound section.

Read notification channel settings

Users can modify the settings for notification channels, including behaviors such as vibration and alert sound. So if you'd like to know the settings a user has applied to your notification channels, Get the AN_NotificationChannel object by calling either GetNotificationChannel() or GetNotificationChannels()

using SA.Android.OS;
using SA.Android.App;
...

var channels = AN_NotificationManager.GetNotificationChannels();
foreach (var stsChannel in channels) {
    PrintChannelInfo(stsChannel);
}

string channelId = "my_channel_id";
channel = AN_NotificationManager.GetNotificationChannel(channelId);
if(channel == null) {
   PrintChannelInfo(channel);
} 

...
private void PrintChannelInfo(AN_NotificationChannel channel) {
    Debug.Log("channel.Id: " + channel.Id);
    Debug.Log("channel.Name: " + channel.Name);
    Debug.Log("channel.Description: " + channel.Description);
    Debug.Log("channel.Importance: " + channel.Importance);
}

Then, if you detect a channel setting that you believe inhibits the intended behavior for your app, you can suggest the user change it and provide an action to open the channel settings (see the next section).

Delete a notification channel

You can delete notification channels by calling DeleteNotificationChannel(). The following sample code demonstrates how to complete this process:

using SA.Android.OS;
using SA.Android.App;
...

string channelId = "my_channel_id";
AN_NotificationManager.DeleteNotificationChannel(channelId);