Wie zeige ich die Android-Benachrichtigung oben an? setPriority(Notification.PRIORITY_MAX)
als Notification.PRIORITY_MAX ist veraltet, was ist die Alternative?
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification Title")
.setContentText("Notification Text")
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true);
In Android O gab es die Einführung von Benachrichtigungskanälen . Insbesondere definieren Sie Channel mit Konstruktor . In der Dokumentation können Sie den Begriff der Wichtigkeit sehen, und dies ersetzt Priorität.
PRIORITY_MAX Diese Konstante wurde auf API-Ebene 26 nicht mehr unterstützt. Verwenden Sie stattdessen IMPORTANCE_HIGH.
PRIORITY_MAX
int PRIORITY_MAX
Diese Konstante wurde in API-Ebene 26 nicht mehr verwendet. Verwenden Sie stattdessen IMPORTANCE_HIGH.
Höchste Priorität für die wichtigsten Elemente Ihrer Anwendung, für die die Eingabeaufforderung des Benutzers oder dessen Eingabe erforderlich ist.
Konstanter Wert: 2 (0x00000002)
// create ios channel
NotificationChannel iosChannel = new NotificationChannel(IOS_CHANNEL_ID,
IOS_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
iosChannel.enableLights(true);
iosChannel.enableVibration(true);
iosChannel.setLightColor(Color.GRAY);
iosChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(iosChannel);
https://developer.Android.com/reference/Android/app/Notification.html#PRIORITY_MIN
Ausgehend von Android O (API 26) wurde die Priorität auf Benachrichtigungsebene verworfen. Es wurde auf Kanalebene durch Bedeutung ersetzt, und die Benachrichtigungen müssen jetzt in einem bestimmten Kanal abgelegt werden.
Wenn Sie mit Android O oder höher arbeiten, müssen Sie einen Kanal erstellen und die Bedeutung des Kanals definieren.
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
Wenn Sie die Rückkompatibilität mit älteren Android-Versionen beibehalten möchten, müssen Sie die Priorität auf Benachrichtigungsebene festlegen.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
...
.setPriority(Notification.PRIORITY_MAX)
...
Leider wird die Warnung über die PRIORITY_MAX-Einstellung ausgegeben.
Es kann vermieden werden, dass die NotificationCompat-Version der PRIORITY_MAX-Konstante verwendet wird.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
...
.setPriority(NotificationCompat.PRIORITY_MAX)
...
Nur drei Schritte für Android O-Benachrichtigung
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hello";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mNotificationManager.createNotificationChannel(mChannel);
}
mNotificationManager.notify(notificationId, notificationBuilder.build());
Arbeitscode für alle Android-Versionen, um die maximale Priorität festzulegen.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setPriority(Notification.IMPORTANCE_HIGH)
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());