Android 通知不显示操作按钮
Android Notification not displaying action buttons
下面是我用来为媒体播放器应用程序构建通知的方法。目前歌曲信息显示正确,但上一首、播放、下一首动作按钮未显示。
这是调用 buildNotification() 后随通知一起显示的内容;
private void buildNotification(Notification.Action action) {
String ID = "sage.musicplayer.Service.MusicService";
/*Bitmap albumArtBitmap = null;
if(album_art != null) {
try {
albumArtBitmap = Glide.with(getApplicationContext()).asBitmap().load(album_art).into(100, 100).get();
}catch(Exception e) {
e.printStackTrace();
}
}else if(albumID > -1) {
albumArtBitmap = musicUtils.getAlbumArt(albumID);
}else{
albumArtBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.new_album_art);
}*/
Intent intent = new Intent(MusicService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
Intent intentPrev = new Intent(ACTION_PREV);
Intent intentPlayPause;
if(isPng())
intentPlayPause = new Intent(ACTION_PAUSE);
else
intentPlayPause = new Intent(ACTION_PLAY);
Intent intentNext = new Intent(ACTION_NEXT);
PendingIntent pendingPrev = PendingIntent.getActivity(this, 0, intentPrev, PendingIntent.FLAG_IMMUTABLE);
PendingIntent pendingPlayPause = PendingIntent.getActivity(this, 0, intentPlayPause, PendingIntent.FLAG_IMMUTABLE);
PendingIntent pendingNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext());
notification.setContentTitle(songTitle)
.setWhen(System.currentTimeMillis())
.setChannelId(ID)
.setSmallIcon(R.drawable.logo)
.setContentText(songArtist)
.setLargeIcon(musicUtils.getAlbumArt(albumID))
.setDeleteIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(MediaSessionCompat.Token.fromToken(mediaSession.getSessionToken())))
.addAction(R.drawable.prev, "Prev", pendingPrev)
.addAction(R.drawable.playbutton, "Play", pendingPlayPause)
.addAction(R.drawable.next, "Next", pendingNext);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(NOTIFY_ID, notification.build());
}
如果您的可绘制对象(R.drawable.prev 等)是矢量图而不是图像,则无法使用。我今天遇到了同样的问题。
下面是我用来为媒体播放器应用程序构建通知的方法。目前歌曲信息显示正确,但上一首、播放、下一首动作按钮未显示。
这是调用 buildNotification() 后随通知一起显示的内容;
private void buildNotification(Notification.Action action) {
String ID = "sage.musicplayer.Service.MusicService";
/*Bitmap albumArtBitmap = null;
if(album_art != null) {
try {
albumArtBitmap = Glide.with(getApplicationContext()).asBitmap().load(album_art).into(100, 100).get();
}catch(Exception e) {
e.printStackTrace();
}
}else if(albumID > -1) {
albumArtBitmap = musicUtils.getAlbumArt(albumID);
}else{
albumArtBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.new_album_art);
}*/
Intent intent = new Intent(MusicService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
Intent intentPrev = new Intent(ACTION_PREV);
Intent intentPlayPause;
if(isPng())
intentPlayPause = new Intent(ACTION_PAUSE);
else
intentPlayPause = new Intent(ACTION_PLAY);
Intent intentNext = new Intent(ACTION_NEXT);
PendingIntent pendingPrev = PendingIntent.getActivity(this, 0, intentPrev, PendingIntent.FLAG_IMMUTABLE);
PendingIntent pendingPlayPause = PendingIntent.getActivity(this, 0, intentPlayPause, PendingIntent.FLAG_IMMUTABLE);
PendingIntent pendingNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext());
notification.setContentTitle(songTitle)
.setWhen(System.currentTimeMillis())
.setChannelId(ID)
.setSmallIcon(R.drawable.logo)
.setContentText(songArtist)
.setLargeIcon(musicUtils.getAlbumArt(albumID))
.setDeleteIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(MediaSessionCompat.Token.fromToken(mediaSession.getSessionToken())))
.addAction(R.drawable.prev, "Prev", pendingPrev)
.addAction(R.drawable.playbutton, "Play", pendingPlayPause)
.addAction(R.drawable.next, "Next", pendingNext);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(NOTIFY_ID, notification.build());
}
如果您的可绘制对象(R.drawable.prev 等)是矢量图而不是图像,则无法使用。我今天遇到了同样的问题。