我使用MediaStyle进行推送通知,以便我可以显示当前播放的歌曲元数据以及包含暂停按钮。我看到的问题是我的通知不会自动展开以显示暂停按钮,即使我已经包含了setShowActionsInCompactView()。如果我在锁定屏幕上下拉通知,它会展开,然后显示暂停按钮。但我希望它能显示按钮而不会扩展。
如何显示通知的屏幕截图: http://cl.ly/image/3E2D0m403v1b
在向下拉展开后如何显示通知的屏幕截图: http://cl.ly/image/1N1i0G121i2Y
以下是我用于生成通知的代码段:
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Intent intent = new Intent(BROADCAST_PLAYER_STOP);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getResources().getString(R.string.app_name))
.setTicker(tickerString)
.setContentText(contentString)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(icon)
.addAction(R.drawable.ic_media_pause, "", pendingIntent)
.setContentIntent(pi)
.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0)
.setMediaSession(mSession.getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(0)
.setOngoing(true);
startForeground(NOTIFICATION_ID, notification.build());
答案 0 :(得分:8)
要使用NotificationCompat.MediaStyle
,您必须使用android.support.v7.app.NotificationCompat.Builder
- 默认的v4 Builder无法处理v7 MediaStyle