Android
Android

Reputation: 150

NotificationCompat and API checks

Does one need to perform an API check if say we use setGroupAlertBehavior from NotificationCompat on Android OS API 19-25?

Upvotes: 2

Views: 229

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

setGroupAlertBehavior is used to change the notification grouping behavior.

The feature of notification groups is only available on Android 7.0 (API level 24) and above which is not backward compatible so yes you need to put an API level check before using it

From Docs

On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. The user can expand this notification to see each separate notification, as shown in figure 1. To support older versions, which cannot show a nested group of notifications, you must create an extra notification that acts as the summary. This appears as the only notification and the system hides all the others. So this summary should include a snippet from all the other notifications, which the user can tap to open your app.

so to barely imitate the behavior of group notifications on older devices , you need to have separate coding behavior hence you need API level check because for older devices.

Our tests indicate no warning from IDE and no crash when running on OS 4.4.x, 6.x and 7.x. Odd.

NotificationCompat use different method to build the notification for different platform so the int parameter passed to setGroupAltertBehavior will never be used while creating notifications for api below 20 so yes it is safe to use it on API below 20

See when setGroupAltertBehavior is called

 public Builder setGroupAlertBehavior(int groupAlertBehavior) {
        mGroupAlertBehavior = groupAlertBehavior;
        return this;
    }

but mGroupAlertBehavior will never be used on API 19 and below as

 @RequiresApi(19)
    static class NotificationCompatApi19Impl extends NotificationCompatApi16Impl {
        @Override
        public Notification build(Builder b, BuilderExtender extender) {
            NotificationCompatKitKat.Builder builder = new NotificationCompatKitKat.Builder(
                    b.mContext, b.mNotification, b.resolveTitle(), b.resolveText(), b.mContentInfo,
                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate, b.mShowWhen,
                    b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly,
                    b.mPeople, b.mExtras, b.mGroupKey, b.mGroupSummary, b.mSortKey,
                    b.mContentView, b.mBigContentView);
            addActionsToBuilder(builder, b.mActions);
            addStyleToBuilderJellybean(builder, b.mStyle);
            return extender.build(b, builder);
        }


    @RequiresApi(20)
    static class NotificationCompatApi20Impl extends NotificationCompatApi19Impl {
        @Override
        public Notification build(Builder b, BuilderExtender extender) {
            NotificationCompatApi20.Builder builder = new NotificationCompatApi20.Builder(
                    b.mContext, b.mNotification, b.resolveTitle(), b.resolveText(), b.mContentInfo,
                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate, b.mShowWhen,
                    b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly, b.mPeople, b.mExtras,
                    b.mGroupKey, b.mGroupSummary, b.mSortKey, b.mContentView, b.mBigContentView,
                    b.mGroupAlertBehavior);
 // used on API 20+. ^^^^^^^^^^^^^^^^^^^^^ but not below
            addActionsToBuilder(builder, b.mActions);
            addStyleToBuilderJellybean(builder, b.mStyle);
            Notification notification = extender.build(b, builder);
            if (b.mStyle != null) {
                b.mStyle.addCompatExtras(getExtras(notification));
            }
            return notification;
        }

Upvotes: 1

Related Questions