Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification coloured action button Android 10

I'm trying to color the buttons(Action) in Notification like this. enter image description here

So far this is what i'm achieved so far. enter image description here

Below is the code i'm using

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

I'm out of ideas now. Any help will be appreciated.

like image 510
Shahal Avatar asked Oct 01 '19 04:10

Shahal


2 Answers

Code taken from the Android official Dialer app

when you set the action string getString(R.string.decline_call) change that with a call to the method

  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;
  }
like image 104
Aenon Avatar answered Oct 08 '22 15:10

Aenon


I have managed to apply colors for the notification action buttons. Use spannable just like Aeron suggested (code from the Dialer app) and the very important thing is to set USE_FULL_SCREEN_INTENT permission. Otherwise colors are ignored in the dark mode or look incorrect in the non dark mode.

like image 36
Alex Avatar answered Oct 08 '22 16:10

Alex