通知频道-设置后可以更改LightColor吗?

时间:2018-11-13 09:53:43

标签: java android notifications channel led

我正在尝试用JavaScript接口返回的颜色更改setLightColor。不幸的是,NotificationCompat.Builder(context, CHANNEL_ID).setLights在API> = 26上绝对没有影响力,所以我不能使用Intent.putExtra或类似的东西。

设置完成后是否还可以更改?我希望它是动态的。

编辑对于我想要的东西似乎有些误解。我不想触摸Broadcast Reciever。它工作正常。我想更改通知频道。它不是在更新setLightColor(Color.___)

protected void onCreate

String jobColor = someColor; // Will be filled in by other code - different colour every time
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Channel_Name";
    String description = "Channel_Description";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    channel.enableLights(true);
    channel.setLightColor(Color.parseColor(jobColor)); // Dynamically set from above
    channel.enableVibration(true);
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

我的BroadcastReciever-我相信 setLight不适用于API 26或更高版本

public class AlarmReceiver extends BroadcastReceiver {
private final String CHANNEL_ID = "some_channel";
@Override
public void onReceive(Context context, Intent intent) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , new Intent(context, MainPage.class), 0);
    String jobColor = intent.getStringExtra("jobColor");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle("Upcoming Shift!")
            .setContentText("Shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime"))
            .setStyle(new NotificationCompat.BigTextStyle().bigText("You have a shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime")))
            .setLights(Color.parseColor(jobColor), 10000, 1000)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(12345, mBuilder.build());
}
}

2 个答案:

答案 0 :(得分:0)

从createNotificationChannel()描述:

  

这还可以用于还原已删除的频道并更新现有频道的频道        *名称,说明,组和/或重要性。

因此您可以尝试以下操作:

  NotificationManager notificationManager = getSystemService(NotificationManager.class);
            //find created channel
            NotificationChannel channel = notificationManager.getNotificationChannel("id");
            if (channel != null){
                //set new color
                channel.setLightColor(0);
                //update channel
                notificationManager.createNotificationChannel(channel);
            }

答案 1 :(得分:0)

在不删除频道的情况下,无法通过编程方式更改频道颜色通知,重要性等。

因为用户可能手动更改了灯光颜色。

要以编程方式实现此目的,以获取频道并创建具有新ID的新频道。删除旧频道。 如果使用先前的ID创建频道,您的更改将不会反映

供参考检查WhatsApp应用程序尝试从应用程序更改铃声并在左下方的通道中看到x通道已删除消息

来源Android Doc