通知彩色操作按钮 Android 10

Posted

技术标签:

【中文标题】通知彩色操作按钮 Android 10【英文标题】:Notification coloured action button Android 10 【发布时间】:2020-01-30 08:41:42 【问题描述】:

我正在尝试像这样为通知中的按钮(操作)着色。

到目前为止,这就是我迄今为止所取得的成就。

下面是我使用的代码

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());
    

我现在没有主意了。任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

代码取自Android官方Dialer应用

当您设置操作字符串 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;
  

【讨论】:

【参考方案2】:

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

【讨论】:

谢谢!!那是我缺少的部分【参考方案3】:

您可以在通知中使用自定义布局。有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();

请阅读here

【讨论】:

设置 setCustomBigContentViewsetStyle(new NotificationCompat.DecoratedCustomViewStyle()) 后,通知不再显示。还有什么需要注意的吗?【参考方案4】:

像这样使用自定义drawable

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);

【讨论】:

new NotificationCompat.Action() 已弃用。即使使用新方法,图标也没有显示。这不是我想要实现的,我正在尝试为按钮着色而不是向其添加图标。无论如何感谢您的快速回复。

以上是关于通知彩色操作按钮 Android 10的主要内容,如果未能解决你的问题,请参考以下文章

按下网页上的按钮->在移动设备上获取通知[关闭]

收到多个Android通知时,只有最新的通知上的操作按钮有效

Android:单击片段中的按钮时如何通知活动? [复制]

Android 通知操作按钮单击侦听器

Android:关闭通知的通知操作

Android Button 背景无法识别/使用可绘制对象