【问题标题】:Custom Android notification with RemoteView and actions带有 RemoteView 和操作的自定义 Android 通知
【发布时间】:2019-01-16 17:31:53
【问题描述】:

我在使用RemoteViews 显示通知并使用addAction(R.drawable.ic_notification, "More", pendingIntent) 在其上添加操作时遇到问题

这是没有addAction 方法的远程视图的通知

这是带有 addAction 的通知

如您所见,系统不显示 RemoteView,只显示操作按钮

这是远程视图的实例化

RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
notificationLayout.setImageViewResource(R.id.notification_small_iv, R.drawable.logo);
notificationLayout.setTextViewText(R.id.notification_small_title, remoteMessage.getData().get(TITLE));
notificationLayout.setTextViewText(R.id.notification_small_subtitle, remoteMessage.getData().get(CONTENT));

这是我设置配置并添加操作的通知生成器

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setCustomContentView(notificationLayout)
            .setColor(getResources().getColor(R.color.nice_blue))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setLights(Color.BLUE, 1000, 1000)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setPriority(PRIORITY_MAX)
            .setDefaults(DEFAULT_ALL)
              .addAction( 0,"More", pendingIntent)
            .build();

这是我创建待处理 Intent 的方式,但我不确定是否相关

PendingIntent pendingIntent;
Intent intent = new Intent(this, SplashActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

我很确定可以有一个自定义视图和一个 AddAction 按钮,因为 Slack 在 Android 应用程序上有它

【问题讨论】:

  • 有趣的问题 - 您的代码在运行 Oreo 的模拟器上工作(展开通知后可以看到自定义视图)但在运行 Lollipop 的模拟器上不起作用(如您所描述的)。(我使用过androidx 库——你使用/导入了哪些库和类?)
  • 我在 >= Oreo 和 Lolipop 上有这个问题我可以看到两个版本的自定义视图,我使用 android.widget.RemoteViews 和 android.support.v4.app.NotificationCompat 之类的 android 导入但是在这两个版本上我都有这个问题

标签: android android-notifications


【解决方案1】:

我在同一条船上。我想要一个顶部的自定义视图,底部的操作按钮选项。你要找的是NotificationCompat.DecoratedCustomViewStyle.

Here is an example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多