Выводить уведомление на экране блокировки
Есть сервис, который запускает аудио в фоновом режиме и выводит уведомление в шторку. На Android < 8 если не ошибаюсь (сейчас не могу проверить) на экране блокировки отображается уведомление, а на Android 9 и выше не отображается. Как выводить уведомление на экран блокировки на Android 9 и выше?
На телефоне Mi9T с Android 10 при тестировании не отображается уведомление на экране блокировки. Уведомления из других приложений выводятся.
public class PlayerService extends Service {
public static Context context;
boolean isPlay = false;
String stream;
String AUDIO;
String img;
Notification notification;
NotificationManager notificationManager;
public static final String KEY_STREAM = PlayerService.class.getSimpleName() + ".KEY_STREAM";
public static final String KEY_AUDIO = PlayerService.class.getSimpleName() + ".KEY_AUDIO";
public static final String KEY_IMG = PlayerService.class.getSimpleName() + ".KEY_IMG";
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(PlayerConstants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent closeIntent = new Intent(this, PlayerService.class);
closeIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0);
int notifyId = 1;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
notification = new NotificationCompat.Builder(this)
.setContentTitle("Играет: " + AUDIO)
.setContentText("Открыть список")
.setSmallIcon(R.mipmap.ic_launcher)
.setDeleteIntent(pCloseIntent)
.setContentIntent(pendingIntent).build();
startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
}
if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)) {
notification = new NotificationCompat.Builder(this)
.setContentTitle("Играет: " + AUDIO)
.setContentText("Открыть список")
.setSmallIcon(R.mipmap.ic_launcher)
.addAction(R.drawable.icon_stop, "Стоп", pCloseIntent)
.setContentIntent(pendingIntent).build();
startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = "cAUDIOID";
CharSequence cannelName = "AUDIO Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelID, cannelName, 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);
Notification notification = new NotificationCompat.Builder(PlayerService.this, channelID)
.setContentTitle("")
.setContentText("Играет: " + AUDIO)
.setColor(Color.RED)
.setSmallIcon(R.mipmap.ic_launcher)
.setChannelId(channelID)
.addAction(R.drawable.icon_stop, "Стоп", pCloseIntent)
.setContentIntent(pendingIntent).build();
startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this;
this.stream = intent.getStringExtra(KEY_STREAM);
this.AUDIO = intent.getStringExtra(KEY_AUDIO);
this.img = intent.getStringExtra(KEY_IMG);
if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STARTFOREGROUND_ACTION)) {
isPlay = true;
showNotification();
AUDIOPlayer.startPlayer(stream, this);
} else if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STOPFOREGROUND_ACTION)) {
isPlay = false;
Intent brIntent = new Intent(PlayerActivity.BROADCAST_ACTION);
brIntent.putExtra(PlayerActivity.SERVICE_PARAM, 0);
sendBroadcast(brIntent);
AUDIOPlayer.stopPlayer();
stopForeground(true);
PlayerActivity.isPlay = false;
stopSelf();
}
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
AUDIOPlayer.stopPlayer();
stopForeground(true);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}