带有 DecoratedCustomViewStyle 的 Android 通知

fin*_*nki 3 android android-appcompat android-notifications android-7.0-nougat

我正在尝试使用自定义布局在 Android 7 上创建通知,但我想使用 v7 支持库中的 DecoratedCustomView 样式:https : //developer.android.com/reference/android/support/v7/app/ NotificationCompat.DecoratedCustomViewStyle.html

我想使用这种风格的原因是我想使用android提供的通知头,如文档所述:

与提供完全自定义的通知不同,开发人员可以设置此样式并仍然获得系统装饰,例如带有扩展功能和操作的通知标题。

因此,我尝试使用仅包含带有单个 TextView 的 LinearLayout 的 RemoteViews。

    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);
Run Code Online (Sandbox Code Playgroud)

结果是一个只包含我的 RemoteViews 的通知,不幸的是没有标题。我刚刚在媒体上找到了一个使用此示例的示例:https : //medium.com/@britt.barak/notifications-part-3-going-custom-31c31609f314

但我无法使用 Android 提供的标头。任何帮助,将不胜感激 :)

Mik*_*e T 5

这似乎是 android 支持库中的一个问题。我用 Notification Builder 测试了它,它的工作原理应该是这样。

我使用了该指南:https : //medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

我在官方谷歌问题跟踪器上为该问题提交了一个错误:https : //issuetracker.google.com/issues/62475846

更新

虽然这不是谷歌方面的实际错误,但我认为实现并不理想。问题是,使用了 v4 支持库的 NotificationCompat,它不适用于 v7 装饰器。

发生这种用法是因为您不能以与 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);
Run Code Online (Sandbox Code Playgroud)

更新 2

由于支持库版本26.0.0的最终发布, 不再需要使用支持库的 v7。DecoratedCustomViewStyle() 现在也在 v4 版本中可用。所以在你的情况下你应该这样做:

.setStyle(new NotificationCompat.DecoratedCustomViewStyle()) 
Run Code Online (Sandbox Code Playgroud)

代替

.setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle())
Run Code Online (Sandbox Code Playgroud)

现在应该做的伎俩。