I am trying to use custom LED color (e.g. using Color.RED
) for notification. I am calling class function UtilsMinimal.showNotification(...)
providing context from BroadcastReceiver
.
I am using the new NotificationChannel
API (yes, I tried the deprecated one too including notification.defaults = 0
and 'notification.argb = ...`).
Whatever I try, when I run program, the LED is pink (I don't event dream to make it blink). No other program is using LED.
What should I do? Messenger, Skype, Viber and other apps do use blue, pink and green color for LED notifications successfully.
...
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioAttributes;
import android.os.Build;
import android.provider.Settings;
public class UtilsMinimal {
private static final String DEFAULT_ID = "default";
// https://www.programcreek.com/java-api-examples/?code=rongcloud%2Fsealrtc-android%2Fsealrtc-android-master%2Fapp%2Fsrc%2Fmain%2Fjava%2Fcn%2Frongcloud%2Frtc%2Fupdateapk%2FUpdateService.java
private static Notification.Builder createNotification(
Context context, String title, String content, Integer progressPercent) {
boolean isLollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
int smallIcon =
context.getResources()
.getIdentifier(
"notification_small_icon", "drawable", context.getPackageName());
if (smallIcon <= 0 || !isLollipop) {
smallIcon = context.getApplicationInfo().icon;
}
Notification.Builder builder = new Notification.Builder(context, DEFAULT_ID);
builder.setSmallIcon(smallIcon);
builder.setCategory(Notification.CATEGORY_ALARM);
if (progressPercent != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_PROGRESS);
}
builder.setProgress(100, progressPercent, false);
}
builder.setContentTitle(title);
if (content != null)
builder.setContentText(content);
builder.setAutoCancel(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(DEFAULT_ID);
}
return builder;
}
static void showNotification(
String title,
String content,
Context ctx,
boolean vibrate,
int ledColor) {
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mBuilder = createNotification(ctx, title, content, null);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
DEFAULT_ID,
"NOTIF_CHANNEL",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("CHANNEL FOR INFORMING ABOUT MESSAGE RECEIVED");
channel.enableLights(true);
channel.setLightColor(ledColor);
channel.shouldShowLights();
if (vibrate) {
long[] vibrationPattern = new long[]{300, 300, 600, 600, 500, 100};
channel.setVibrationPattern(vibrationPattern);
channel.enableVibration(true);
}
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, att);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(DEFAULT_ID);
}
Notification notification = mBuilder.build();
// notification.defaults = 0;
// notification.ledARGB = ledColor;
mNotificationManager.notify(0, notification);
}
}