Android Notification with DecoratedCustomViewStyle

2020-07-26 15:47发布

问题:

I'm trying to create a Notification on Android 7 with a custom layout but I want to use the DecoratedCustomView Style from the v7-Support Library: https://developer.android.com/reference/android/support/v7/app/NotificationCompat.DecoratedCustomViewStyle.html

The reason why I want to use this style is that I want to use the notification header provided by android, as the documentation states:

Instead of providing a notification that is completely custom, a developer can set this style and still obtain system decorations like the notification header with the expand affordance and actions.

So I tried it with RemoteViews only containing a LinearLayout with a single TextView.

    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setWhen(new Date().getTimeInMillis())
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("title")
                .setContentText("text")
                .setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle())
                .setContent(remoteViews);

The result is a Notification just containing my RemoteViews and no header unfortunately. I just found one example using this on medium: https://medium.com/@britt.barak/notifications-part-3-going-custom-31c31609f314

But I'm not able to use the header provided by Android. Any help would be appreciated :)

回答1:

It seems like this is an issue in the android support library. I tested it with the Notification Builder and it works like it should.

I used that guide: https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

I filed a bug for that issue at the official google issue tracker: https://issuetracker.google.com/issues/62475846

update

While this is not an actual bug on googles side, I think the implementation is not really ideal. The problem is, that the NotificationCompat of the v4 support lib is used, which does not work with the v7 decorator.

This usage happens because you cannot use the builder pattern the same way with v7 NotificationCompat.

import android.support.v7.app.NotificationCompat;

final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setWhen(new Date().getTimeInMillis())
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("title")
            .setContentText("text")
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setContent(remoteViews);

update 2

Since the final release of the support library version 26.0.0 There is no need for using the v7 of the support library anymore. The DecoratedCustomViewStyle() is now available in the v4 version as well. So in your case you should do:

.setStyle(new NotificationCompat.DecoratedCustomViewStyle()) 

instead of

.setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle())

Should now do the trick.