gpt4 book ai didi

android - 尽管创建了 channel ,但通知未显示在 Android O 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:36 25 4
gpt4 key购买 nike

这是我在下面给出的代码。这无法在 Android O 上创建任何通知,尽管创建了通知 channel 。

private void weatherNotification(WeatherInfo weather) {
Intent intent = new Intent(this, WeatherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

String temperatureScale = prefs.getUnits().equals("metric") ? getString(R.string.c) : getString(R.string.f);
String speedScale = prefs.getUnits().equals("metric") ? getString(R.string.mps) : getString(R.string.mph);

String temperature = getString(R.string.temperature , weather.getMain().getTemp() , temperatureScale);
String city = getString(R.string.city , weather.getName() + ", " + weather.getSys().getCountry());
String wind = getString(R.string.wind_ , weather.getWind().getSpeed(), speedScale);
String humidity = getString(R.string.humidity , weather.getMain().getHumidity());
String pressure = getString(R.string.pressure, weather.getMain().getPressure());

String data = city + "\n" + temperature + "\n" + wind + "\n" + humidity + "\n" + pressure;

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = "w01", name = getString(R.string.weather_notification_title);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
String desc = getString(R.string.weather_notification_description);

NotificationChannel channel = new NotificationChannel(id, name, importance);
channel.setDescription(desc);
notificationManager.createNotificationChannel(channel);
}
Notification.Builder builder = new Notification.Builder(this);

builder.setAutoCancel(false);
builder.setContentTitle("Weather Notification");
builder.setContentText(Math.round(weather.getMain().getTemp()) + temperatureScale + " at " + weather.getName());
builder.setStyle(new Notification.BigTextStyle().bigText(data));
builder.setSmallIcon(R.drawable.ic_notification_icon);
builder.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= 24)
builder.setColor(Color.parseColor("#ff0000"));
Notification notification = builder.build();
notificationManager.notify(0 , notification);
}

我相信我已经遵循了在 Android O 上创建通知所需的所有步骤 - 使用通知管理器创建通知 channel ,然后在此管理器上构建通知。我不知道我哪里出错了。

编辑 1:在 weatherNotification() 方法中做了以下更改,仍然不起作用:

private void weatherNotification(WeatherInfo weather) {

....

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = "w01", name = getString(R.string.weather_notification_title);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
String desc = getString(R.string.weather_notification_description);

NotificationChannel channel = new NotificationChannel(id, name, importance);
channel.setDescription(desc);
notificationManager.createNotificationChannel(channel);
builder = new Notification.Builder(this , id);
}
builder = new Notification.Builder(this);
builder.setAutoCancel(false);
builder.setContentTitle("Weather Notification");
builder.setContentText(Math.round(weather.getMain().getTemp()) + temperatureScale + " at " + weather.getName());
builder.setStyle(new Notification.BigTextStyle().bigText(data));
builder.setSmallIcon(R.drawable.ic_notification_icon);
builder.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= 24)
builder.setColor(Color.parseColor("#ff0000"));
Notification notification = builder.build();
notificationManager.notify(0 , notification);
....
}

编辑 2:从编辑 1 代码中,我发现生成器再次被重新生成。所以我又做了如下修改:

private void weatherNotification(WeatherInfo weather) {
....
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = "w01", name = getString(R.string.weather_notification_title);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
String desc = getString(R.string.weather_notification_description);

NotificationChannel channel = new NotificationChannel(id, name, importance);
channel.setDescription(desc);
notificationManager.createNotificationChannel(channel);
builder = new Notification.Builder(this , id);
}
else
builder = new Notification.Builder(this);
....
}

最佳答案

您应该在这一行收到弃用警告:

Notification.Builder builder = new Notification.Builder(this);

那是因为新构造函数获取了您的 channel ID:

Notification.Builder builder = new Notification.Builder(this, id);

(尽管您需要稍微修改代码以便 id 仍然可用。

引用 the JavaDocs for the constructor that you are using right now :“所有发布的通知必须指定一个 NotificationChannel Id。”就目前而言,您在发出 Notification 时并未使用该 channel 。据我所知,这将阻止显示您的通知

关于android - 尽管创建了 channel ,但通知未显示在 Android O 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46255675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com