Anzeige mit zwei Protokollen
1: Die Verwendung von Stream-Typen ist für andere Vorgänge als die Datenträgerkontrolle veraltet
2: Informationen zur Verwendung von Android.media.AudioAttributes finden Sie in der Dokumentation zu setSound ()
Wenn Sie Android 8.0 (API-Level 26) als Ziel verwenden, müssen Sie einen oder mehrere Benachrichtigungskanäle implementieren, um Benachrichtigungen für Ihre Benutzer anzuzeigen.
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message);
Intent resultIntent = new Intent(ctx, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Gulzar Bhats Antwort funktioniert perfekt, wenn Sie mindestens Oreo als API verwenden. Wenn Ihr Minimum jedoch niedriger ist, müssen Sie den NotificationChannel-Code in eine Prüfung auf Plattformebene einschließen. Danach können Sie immer noch die ID verwenden, die vor Oreo ignoriert wird:
if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());
Sie können dieses Problem auf zwei Arten lösen, aber für beide müssen Sie einen Benachrichtigungskanal mit einer bestimmten Kanal-ID erstellen.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
mChannel.enableLights(true);
mNotificationManager.createNotificationChannel(mChannel);
Die erste Möglichkeit ist, den Kanal für die Benachrichtigung im Konstruktor festzulegen:
Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title");
mNotificationManager.notify("your_notification_id", notification);
Die zweite Möglichkeit besteht darin, den Kanal durch Notificiation.Builder.setChannelId () festzulegen.
Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title").
setChannelId(id);
mNotificationManager.notify("your_notification_id", notification);
Hoffe das hilft
Erstellen Sie zuerst den Benachrichtigungskanal:
public static final String NOTIFICATION_CHANNEL_ID = "4655";
//Notification Channel
CharSequence channelName = NOTIFICATION_CHANNEL_NAME;
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
verwenden Sie dann die Kanal-ID im Konstruktor:
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_timers)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setSound(null)
.setContent(contentView)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setLargeIcon(picture)
.setTicker(sTimer)
.setContentIntent(timerListIntent)
.setAutoCancel(false);
Dieses Problem hängt mit der alten FCM-Version zusammen.
Aktualisieren Sie Ihre Abhängigkeit auf com.google.firebase:firebase-messaging:15.0.2
Oder höher.
Dies wird den Fehler beheben
failed to post notification on channel null
wenn Ihre App Benachrichtigungen im Hintergrund erhält, da Firebase jetzt einen Standard-Benachrichtigungskanal mit Grundeinstellungen bereitstellt.
Sie können aber auch den Standard-Benachrichtigungskanal für FCM im Manifest angeben.
<meta-data
Android:name="com.google.firebase.messaging.default_notification_channel_id"
Android:value="@string/default_notification_channel_id"/>
Um mehr zu erfahren hier
String CHANNEL_ID = "my_channel";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);
Wenn Sie diese Fehlermeldung erhalten, sollten Sie auf 2 Artikel achten und diese bestellen:
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
builder = new NotificationCompat.Builder(this, id);
Auch NotificationManager notifManager und NotificationChannel mChannel werden nur einmal erstellt.
Für die Benachrichtigung sind folgende Setter erforderlich:
builder.setContentTitle() // required
.setSmallIcon() // required
.setContentText() // required
Siehe das Beispiel in On Android 8.1 API 27-Benachrichtigung wird nicht angezeigt .
Ich hatte ein ähnliches Problem, aber einen Dienst als Hintergrundaktivität zu starten, im Dienst funktionierte dieser Code:
public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1001;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = "_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, "notification", importance);
mChannel.enableLights(true);
Notification notification = new Notification.Builder(getApplicationContext(), id)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My chat")
.setContentText("Listening for incoming messages")
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager.notify(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
}
startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
}
}
Es ist nicht die beste Lösung, sondern nur um den Dienst im Hintergrund zu starten. Es funktioniert