Notification Channel - LightColor设置后是否可以更改?

Notification Channel - Is It Possible to Change LightColor After It's Been Set?

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

设置好之后还可以修改吗?我希望它是动态的。

EDIT 似乎对我想要的有一些误解。我不想碰 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());
}
}

来自 createNotificationChannel() 描述:

This can also be used to restore a deleted channel and to update an existing channel's * name, description, group, and/or importance.

所以你可以试试这个:

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

您不能在不删除频道的情况下以编程方式更改频道颜色通知、重要性等。

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

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

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

来源Android Doc