在紧凑视图中显示按钮的 MediaStyle 通知
MediaStyle Notification to Show Button in Compact View
我将 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());
要使用 NotificationCompat.MediaStyle
,您必须使用 android.support.v7.app.NotificationCompat.Builder
- 默认的 v4 Builder 不处理 v7 MediaStyle
我将 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());
要使用 NotificationCompat.MediaStyle
,您必须使用 android.support.v7.app.NotificationCompat.Builder
- 默认的 v4 Builder 不处理 v7 MediaStyle