Android Oreo Notification Channels

JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai
edited November 8 in Xamarin Blog

Have you tried using Android Oreo's Notification Channels yet?

If you haven't, read our blog for a quick primer on Notification Channels and let us know what you think!

Feel free to discuss this post below.

Tagged:

Posts

  • EricBrunnerEricBrunner USMember ✭✭
    Hi

    I miss the following

    1. I have a XF Droid/iOS solution which supports min. SDK API LEVEL 16 in Droid. Now when I upgrade to API-LEVEL 26/27 OREO I am forced to use Notification Channels. But what about my backward compatibility on devices that don't have OREO installed (Android OS < API-LEVEL 26)

    thanks
    Eric
  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    @EricBrunner

    Only Oreo and newer versions of Android require that Notifications have proper channels defined. There are two scenarios that come in mind:

    1. Your app does not target Android 8.0 (API 26) but is ran against a device running Android 8.0 (API 26)

    This situation it will behave as if it were on <= Android 7.1 (API 25)

    1. Your app does target Android 8.0 (API 26) but is ran against a device running < Android 8.0 (API 26)

    This situation will be completely ignored as if it didn't exist. No need to worry about the current implementation breaking.

    So in both situations, your apps that are not running Android 8.0 or greater, these APIs will not be leveraged and it should remain compatible if you do or do not have Notification Channel implementations.

  • EricBrunnerEricBrunner USMember ✭✭

    @JonDouglas
    Hi Jon,
    Thanks for getting back. I am still confused.

    That is my current code I use to create notification on my Droid XF app targeting API-LEVEL 25 (Android 7.1).

    private void CreateNotification(Context context, string desc)
    {
    var notificationManager = GetSystemService(NotificationService) as NotificationManager;
    var uiIntent = new Intent(context, typeof(MainActivity));

            //activity will not be launched if it is already running at the top of the history stack.
            uiIntent.AddFlags(ActivityFlags.SingleTop);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    
            var notification = builder
                .SetContentIntent(PendingIntent.GetActivity(context, 0, uiIntent, 0))
                .SetSmallIcon(Android.Resource.Drawable.SymDefAppIcon)
                .SetTicker("TruckerApp")
                .SetContentTitle("TruckerApp")
                .SetContentText(desc)
                .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                .SetAutoCancel(true)
                .Build();
    
            notificationManager?.Notify(1, notification);
        }
    

    I would like to target >= API-LEVEL 26 (Android 8) . Now I found that SO answer. Could you provide the change I would have to do in my code to get notifications properly on Android OS below API-LEVEL 26 and above ?

    SO answer

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    @EricBrunner

    There is no change you need to make for APIs < Oreo. However you need to set a channel in order for APIs >= Oreo to work. Thus if you wanted to, you can follow a similar idea here:

    https://stackoverflow.com/a/44534559/1048571

    Google also recommends this:

    Apps targeting the Android 8.0 (API level 26) version of the SDK must implement one or more notification channels to post notifications to users.

Sign In or Register to comment.