【问题标题】:Notification Channel - Is It Possible to Change LightColor After It's Been Set?通知通道 - 设置后是否可以更改 LightColor?
【发布时间】:2019-04-16 02:58:28
【问题描述】:

我正在尝试使用从 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());
}
}

【问题讨论】:

  • 每次当您想更新当前内容时更新通知。

标签: java android notifications channel led


【解决方案1】:

如果不删除频道,您将无法以编程方式更改频道颜色通知、重要性等。

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

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

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

来源Android Doc

【讨论】:

  • 是的,就是这样。我想可能还有另一种方式。谢谢。
【解决方案2】:

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

【讨论】:

  • 这会引发错误,因为 getNotificationChannel 在第一次运行时变为 null。我只是将getNotificationChannel(CHANNEL_ID) 放在 if 中,然后它就运行了。但是,这无论如何都不起作用。 if 中有一个 sys.out,所以我可以判断 if 是否运行,它确实运行。但颜色变化不适用。
猜你喜欢
  • 2019-09-06
  • 1970-01-01
  • 2019-07-11
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2016-04-08
相关资源
最近更新 更多