A notificação do Android Oreo trava a interface do sistema
Consegui receber notificações trabalhando em APIs mais antigas, mas não no Oreo. A criação da notificação faz com que meu aplicativo ainda funcione bem (sem mensagens no logcat), no entanto, o SystemUI trava e reinicia em um ciclo sem fim enquanto a Atividade está em execução. É o erro no logcat para o processo systemui:
java.lang.IllegalArgumentException: width and height must be > 0
Meu código:
private void showPlayingNotification() {
NotificationCompat.Builder builder = mNotificationUtils.getAndroidChannelNotification(this, "Play", mMediaSessionCompat);
if( builder == null ) {
Log.i("Play Notification","No notification found!");
return;
}
mNotificationUtils.getManager().notify(101,builder.build());
}
Inicializei o mNotificationUtils no onCreate do MediaPlayerService que criei.
public class NotificationUtils extends ContextWrapper {
private NotificationManager mManager;
public static final String AUDIO_CHANNEL_ID = "com.liftyourheads.dailyreadings.dailyReadingsAudio";
public static final String AUDIO_CHANNEL_NAME = "Daily Readings Audio Stream";
public NotificationUtils(Context base) {
super(base);
createChannels();
}
public void createChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// create android channel
NotificationChannel dailyReadingsAudioChannel = new NotificationChannel(AUDIO_CHANNEL_ID,
AUDIO_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(dailyReadingsAudioChannel);
}
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getAndroidChannelNotification(Context context, String action, MediaSessionCompat mediaSession) {
if (action.equals("Play")) {
return MediaStyleHelper.from(context, mediaSession)
.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
.setStyle(
new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0)
.setMediaSession(mediaSession.getSessionToken()))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Content Text")
.setContentTitle("Content Title")
.setChannelId(AUDIO_CHANNEL_ID);
} else if (action.equals("Pause")) {
return MediaStyleHelper.from(context, mediaSession)
.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
.setStyle(
new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0)
.setMediaSession(mediaSession.getSessionToken()))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Content Text")
.setContentTitle("Content Title")
.setChannelId(AUDIO_CHANNEL_ID);
}
return null;
} }