英文:
Mediastyle notifications not working in android 11
问题
以下是您的代码翻译部分:
public static final String CHANNEL_ID = "Channel1";
//public static final String ACTION_PREVIOUS = "actionprevious";
public static final String ACTION_PLAY = "actionplay";
public static final String ACTION_EXIT = "actionexit";
//public static final String ACTION_NEXT = "actionnext";
public static Notification notification;
public static void createNotification(Context context, Track track, int playbutton, int exitApp, int pos, int size) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag");
mediaSessionCompat.setActive(true);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.half);
Intent intentPlay = new Intent(context, NotificationActionService.class)
.setAction(ACTION_PLAY);
PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intentExit = new Intent(context, NotificationActionService.class)
.setAction(ACTION_EXIT);
PendingIntent pendingIntentExit = PendingIntent.getBroadcast(context, 0,
intentExit, PendingIntent.FLAG_UPDATE_CURRENT);
exitApp = R.drawable.ic_close_black;
Intent intentOpenApp = new Intent(context, MainActivity.class);
PendingIntent pendingIntentOpenApp = PendingIntent.getActivity(context, 0,
intentOpenApp, 0);
Notification.MediaStyle style = new Notification.MediaStyle();
androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
//NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
.setSmallIcon(R.drawable.ic_audiotrack)
.setVisibility(androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC)
.setLargeIcon(icon)
.setContentTitle("TEST")
.setContentText(notificationText)
.setContentIntent(pendingIntentOpenApp)
.setShowWhen(false)
.setOngoing(true)
.setBadgeIconType(androidx.core.app.NotificationCompat.BADGE_ICON_NONE)
.setOnlyAlertOnce(true)
.addAction(action)
.addAction(generateAction(R.drawable.ic_close_black, "Exit", ACTION_EXIT))
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1)
.setMediaSession(mediaSession.getSessionToken()));
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, icon)
.putString(MediaMetadata.METADATA_KEY_TITLE, "TEST TITLE")
.putString(MediaMetadata.METADATA_KEY_ARTIST, "TEST ARTIST")
.build());
startForeground(1, builder.build());
}
}
注意:此翻译只包括代码部分,不包含任何问题回答或其他内容。
英文:
My custom mediastyle notification no longer works in Android 11 for my music app. It works fine in Android 10 and previous versions.
Is there any other code I need to add so that it works in Android 11.
I should add that getting rid of the " .setMediaSession(mediaSessionCompat.getSessionToken())) " line gives me a notification, but its not an Oreo notification with the full background color, etc.
Here is my code for creating notifications:
public static final String CHANNEL_ID = "Channel1";
//public static final String ACTION_PREVIOUS = "actionprevious";
public static final String ACTION_PLAY = "actionplay";
public static final String ACTION_EXIT = "actionexit";
//public static final String ACTION_NEXT = "actionnext";
public static Notification notification;
public static void createNotification(Context context, Track track, int playbutton, int exitApp, int pos, int size) {
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag"); /
mediaSessionCompat.setActive(true);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.half);
Intent intentPlay = new Intent(context, NotificationActionService.class)
.setAction(ACTION_PLAY);
PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intentExit = new Intent(context, NotificationActionService.class)
.setAction(ACTION_EXIT);
PendingIntent pendingIntentExit = PendingIntent.getBroadcast(context, 0,
intentExit, PendingIntent.FLAG_UPDATE_CURRENT);
exitApp = R.drawable.ic_close_black;
Intent intentOpenApp = new Intent(context, MainActivity.class);
PendingIntent pendingIntentOpenApp = PendingIntent.getActivity(context,0,
intentOpenApp, 0);
Notification.MediaStyle style = new Notification.MediaStyle();
androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
//NotificationCompat.Builder builder = new NotificationCompat.Builder( this, NOTIFICATION_CHANNEL_ID );
.setSmallIcon(R.drawable.ic_audiotrack)
.setVisibility(androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC)
.setLargeIcon(icon)
.setContentTitle( "TEST" )
.setContentText(notificationText)
.setContentIntent(pendingIntentOpenApp)
.setShowWhen(false)
.setOngoing(true) .setBadgeIconType(androidx.core.app.NotificationCompat.BADGE_ICON_NONE)
.setOnlyAlertOnce(true)
.addAction(action)
.addAction(generateAction(R.drawable.ic_close_black, "Exit", ACTION_EXIT))
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0,1)
.setMediaSession(mediaSession.getSessionToken()));
mediaSession.setMetadata
(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,icon)
.putString(MediaMetadata.METADATA_KEY_TITLE, "TEST TITLE")
.putString(MediaMetadata.METADATA_KEY_ARTIST, "TEST ARTIST")
.build()
);
startForeground(1, builder.build());
}
}
答案1
得分: 10
你现在还需要向媒体会话添加元数据:
mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
.build()
);
英文:
You now need to add metadata to your media session as well:
mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
.build()
);
答案2
得分: 2
我遇到了同样的问题,只有当我将compileSdkVersion设置为30时,我才能解决它。
英文:
I had same issue and was able to resolve it only once I set compileSdkVersion to 30.
答案3
得分: 0
刚刚遇到了同样的问题。事实证明,对于Mediastyle通知,.setOngoing(true)
似乎无法正常工作。一旦我将其移除,通知就能正确显示。
英文:
Just had the same problem. Turns out .setOngoing(true)
doesn't seem to work right with Mediastyle notifications. Once I removed it, the notification showed up correctly.
答案4
得分: 0
这有一个简单的解决方法。只需在 mediaSessionCompat 声明后添加以下几行代码。
mediaSessionCompat = new MediaSessionCompat(this, "tag");
mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, "歌曲标题")
.putString(MediaMetadata.METADATA_KEY_ARTIST, "艺术家")
.build());
希望对你有所帮助。
英文:
There is a simple solution to it. Just add these lines after mediaSessionCompat declaration.
mediaSessionCompat=new MediaSessionCompat(this,"tag");
mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE,"Song Title")
.putString(MediaMetadata.METADATA_KEY_ARTIST,"Artist")
.build());
Hope this helps.
答案5
得分: 0
在 setStyle(..)
后添加 mMediaSession.setMetadata(..)
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
...
mBuilder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mMediaSession.getSessionToken())
.setShowCancelButton(true)
.setShowActionsInCompactView(0, 1, 2)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
context, PlaybackStateCompat.ACTION_STOP)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_previous, "Previous",
ppreviousIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_pause, "Pause",
pplayIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_next, "Next",
pnextIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_close, "Close",
pcloseIntent));
mMediaSession.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE,"Song Title")
.putString(MediaMetadata.METADATA_KEY_ARTIST,"Artist")
.build());
}
else {
...
}
英文:
Works for me : Should add "mMediaSession.setMetadata(..)" after setStyle(..)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
...
mBuilder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mMediaSession.getSessionToken())
.setShowCancelButton(true)
.setShowActionsInCompactView(0, 1, 2)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
context, PlaybackStateCompat.ACTION_STOP)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_previous, "Previous",
ppreviousIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_pause, "Pause",
pplayIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_next, "Next",
pnextIntent))
.addAction(new NotificationCompat.Action(
R.drawable.ic_noti_close, "Close",
pcloseIntent));
mMediaSession.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE,"Song Title")
.putString(MediaMetadata.METADATA_KEY_ARTIST,"Artist")
.build());
else
{
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论