Dies ist mein unten angegebener Code. Auf Android O können keine Benachrichtigungen erstellt werden, obwohl der Benachrichtigungskanal erstellt wurde.Benachrichtigung wird nicht in Android O angezeigt, obwohl ein Kanal erstellt wurde
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);
}
Ich glaube, ich habe alle Schritte befolgt, die eine Benachrichtigung für die Erstellung von auf Android O erforderlich sind, - Erstellen von Benachrichtigungskanal mit dem Benachrichtigungsmanager, gefolgt von der Meldung über diesen Manager zu bauen. Ich weiß nicht, wo ich falsch liege.
EDIT 1: Made folgende Änderung in weatherNotification() -Methode, immer noch nicht funktioniert:
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);
....
}
EDIT 2: Von Edit 1-Code, fand ich heraus, dass der Erbauer wieder regeneriert wurde. Also machte ich folgende Änderungen wieder:
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);
....
}
Bitte überprüfen EDIT 1 – Sparker0i
EDIT 2, es hat funktioniert..Danke – Sparker0i