Search code examples
androidpush-notificationfirebase-cloud-messagingandroid-push-notification

What is the purpose of the condition "if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {" in the Firebase Cloud Messaging sample project?


I am implementing the Firebase Cloud Messaging Quickstart sample project available at https://github.com/firebase/quickstart-android/tree/master/messaging, incorporating it to my app. In https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MainActivity.java I can see the following block of code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create channel to show notifications.
    String channelId  = getString(R.string.default_notification_channel_id);
    String channelName = getString(R.string.default_notification_channel_name);
    NotificationManager notificationManager =
            getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(new NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_LOW));
}

What is the purpose of using the condition if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){}? From what I understand, Build.VERSION.SDK_INT returns the API Level of the device where the app is installed, and Build.VERSION_CODES.O is what I define as the API level to compile against in the app/build.gradle file, for example: compileSdkVersion 26. Is the code asking to not execute the code that creates the channel to show notifications, if the user has a device with an API level that is lower than the compileSdkVersion that I am using to define which SDK version I am compiling against? I am not understanding the purpose of that condition. By the way, I am testing with a phone whose API Level is 23 and expected, since I am using compileSdkVersion 26 in my build.gradle file, the entire block of code is not being executed. I will appreciate if you could help to clarify the purpose of this code, and of course it is not code that I wrote. I took it from https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MainActivity.java, but I am trying to understand it. Thank you.


Solution

  • What is the purpose of using the condition

    To avoid executing that block of code on devices older than Android 8.0. Notification channels were added in Android 8.0. Attempting to call createNotificationChannel() on older devices would result in a crash, as that method will not exist.

    This is a standard backwards-compatibility recipe. Frequently, utility classes hide this stuff (e.g., most of the classes named ...Compat in the SDK), but sometimes, as is the case here, we get to do it ourselves.

    Is the code asking to not execute the code that creates the channel to show notifications, if the user has a device with an API level that is lower than the compileSdkVersion that I am using to define which SDK version I am compiling against?

    Yes.