gpt4 book ai didi

java - Mediastyle 通知在 android 11 中不起作用

转载 作者:行者123 更新时间:2023-12-04 15:58:20 25 4
gpt4 key购买 nike

我的自定义媒体样式通知不再适用于我的音乐应用的 Android 11。它在 Android 10 和以前的版本中运行良好。
是否需要添加任何其他代码才能在 Android 11 中运行。
我应该补充一点,摆脱“.setMediaSession(mediaSessionCompat.getSessionToken())) ”行会给我一个通知,但它不是具有完整背景颜色的奥利奥通知等。
这是我创建通知的代码:

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());
}


}

最佳答案

您现在还需要向媒体 session 添加元数据:

mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
.build()
);

关于java - Mediastyle 通知在 android 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64090494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com