Hola, estoy configurando una notificación para llamadas entrantes con dos acciones: Responder y Rechazar. Necesito configurar el color verde para la acción de Respuesta y el rojo para Rechazar. Pero no pude encontrar una solución.
Aquí está mi código:
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,"Call") .setSmallIcon(R.drawable.ic_stat_rider_logo) .setContentIntent(contentIntent) .setContentTitle(generalFunc.retrieveLangLBl("","LBL_SINCH_NOTIFICATION_CONTENT")) .setContentText(call.getHeaders().get("Name") +" "+ generalFunc.retrieveLangLBl("","LBL_SINCH_NOTIFICATION_TITLE")); builder.addAction(getServiceNotificationAction(mContext, denyCallIntent(mContext,call),R.drawable.ic_call_icon, R.string.decline)); builder.addAction(getServiceNotificationAction(mContext, answerCallIntent(mContext,call),R.drawable.com_facebook_close, R.string.answer)); if (callActivityRestricted()) { builder.setFullScreenIntent(contentIntent, true); builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setCategory(NotificationCompat.CATEGORY_CALL); } private NotificationCompat.Action getServiceNotificationAction(Context context, Intent intent, int iconResId, int titleResId) { PendingIntent pendingIntent = Build.VERSION.SDK_INT >= 26 ? PendingIntent.getForegroundService(context, 0, intent, 0) : PendingIntent.getService(context, 0, intent, 0); return new NotificationCompat.Action(iconResId, context.getString(titleResId), pendingIntent); }Probé setColor() , pero establece un color único para ambas acciones.
Por favor ayúdame a solucionar esto . gracias de antemano
Probé tu código y lo logré con la ayuda de la clase Spannable.
public void sendOnChannel1(View v) { String title = editTextTitle.getText().toString(); String message = editTextMessage.getText().toString(); Intent activityIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0); Intent broadcastIntent = new Intent(this, NotificationReceiver.class); broadcastIntent.putExtra("toastMessage", message); PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID) .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setContentIntent(contentIntent) .setAutoCancel(true) .setColorized(true) .setOnlyAlertOnce(true) .setPriority(NotificationCompat.PRIORITY_HIGH) .addAction(R.mipmap.ic_launcher, getMyActionText(R.string.answer,R.color.green), actionIntent) .addAction(R.mipmap.ic_launcher, getMyActionText(R.string.decline,R.color.red), actionIntent) .build(); notificationManager.notify(1, notification); }Aquí hay un método de ayuda que utilicé:
private Spannable getMyActionText(@StringRes int stringRes, @ColorRes int colorRes) { Spannable spannable = new SpannableString(this.getText(stringRes)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { spannable.setSpan( new ForegroundColorSpan(this.getColor(colorRes)), 0, spannable.length(), 0); } return spannable; }