Я пытаюсь запустить startForegroundService()
в устройствах Android O
и выше.
И сервис запустился. В методе сервиса onCreate()
я добавил startForeground()
с уведомлением.
Но уведомления не приходит. Я не могу этого видеть.
Мой код в методе сервиса onCreate()
:
Notification.Builder builder = new Notification.Builder(this, "1")
.setContentTitle(getString(R.string.app_name))
.setContentText("in app filling")
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
нет, вы можете сослаться на какую-нибудь ссылку. @ Ümañgßürmån
Извините, я имел ввиду NotificationChannel
:)
Решение:
Шаг 1: Создать NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(channel_id , channel_name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Здесь channel_id и channel_name - это переменные int
и string
соответственно.
Шаг 2: Прикрепите к NotificationManager
:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
Шаг 3: Создайте свое уведомление:
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Test Title")
.setContentText("Test Message")
.setSmallIcon(R.mipmap.ic_launcher);
Шаг 4: Прикрепите уведомление в том же объекте NotificationManager
notificationManager.notify(1, notification.build());
Наконец, поставьте галочку, чтобы узнать, выше ли она Android O:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
....
}
Упомянутые и Подробнее об этом можно найти Здесь
Надеюсь, это поможет.
Начиная с версии Android Oreo, вам необходимо добавить канал в свое уведомление следующим образом:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
и создайте объект для уведомления следующим образом:
Notification.Builder notification = new Notification.Builder(this, "CHANNEL_ID")
В Android O и выше необходим
NotificationId
... Вы еще не пробовали?