通知着色操作按钮 Android 10

13

我试图像这样为通知中的按钮(Action)上色。

进入图片描述

到目前为止,这是我所达到的。

进入图片描述

以下是我使用的代码

NotificationService.class

private void showCallNotification(Map<String, String> dataMap) {
        notificationId = (int) (System.currentTimeMillis() % 10000);
        Intent intent = new Intent(getApplicationContext(), ReceiveCallActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setAction(Intent.ACTION_ANSWER)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_ACCEPTED)
                .putExtra("title", dataMap.get("title"))
                .putExtra("action", dataMap.get("action"));

        Intent cancelIntent = new Intent(getApplicationContext(), VideoCallReceiver.class)
                .setAction(AppConstants.INCOMING_CALL_BROADCAST_ACTION)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_DECLINED)
                .putExtra("notificationId", notificationId);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Action declineAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.decline_call), cancelPendingIntent);
        NotificationCompat.Action acceptAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.accept_call), pendingIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(dataMap.get("sender"))
                .setContentText(getString(R.string.incoming_call))
                .setColor(getResources().getColor(R.color.notification_color))
                .setAutoCancel(true)
                .setTimeoutAfter(CALL_DISMISS_TIME)
                .addAction(declineAction)
                .addAction(acceptAction)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setFullScreenIntent(pendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createCallNotificationChannel();
        }

        notificationManager.notify(notificationId, builder.build());
    }

我现在想不出主意了,任何帮助将不胜感激。

4个回答

18

代码取自Android官方的拨号器应用程序

当您设置操作字符串getString(R.string.decline_call)时,请将其更改为调用该方法

  private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
    Spannable spannable = new SpannableString(context.getText(stringRes));
    if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
      spannable.setSpan(
          new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
    }
    return spannable;
  }

3

我已经成功为通知操作按钮应用颜色。像Aeron建议的那样使用Spannable(来自拨号应用程序的代码),非常重要的是设置USE_FULL_SCREEN_INTENT权限。否则,在深色模式下,颜色会被忽略或在非深色模式下看起来不正确。


谢谢!那就是我缺少的部分。 - Javier

0

您可以在通知中使用自定义布局。有setCustomContentView()setCustomBigContentView方法:

// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNE L_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .build();

请阅读这里

1
在设置了setCustomBigContentViewsetStyle(new NotificationCompat.DecoratedCustomViewStyle())之后,通知不再显示。我还需要注意什么吗? - Shahal

-2

使用自定义绘制对象,可以这样实现

NotificationCompat.Action declineAction = new NotificationCompat.Action(R.drawable.ic_red, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(R.drawable.ic_green, getString(R.string.accept_call), pendingIntent);

1
new NotificationCompat.Action()已被弃用。即使使用了新的方法,图标也不会显示。而且这也不是我想要实现的,我想要的是给按钮上色,而不是添加图标。无论如何,感谢您的快速回复。 - Shahal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接