问题
I'm using the MediaStyle for my push notification so that I can show currently playing song meta data as well as include a pause button. The issue I'm seeing is that my notification is not automatically expanding to show the pause button, even though I've included the setShowActionsInCompactView(). If I pull down on my notification while on the lock screen, it expands and then shows the pause button. But I would like it to show the button without expanding.
Screenshot of how notification is displayed: http://cl.ly/image/3E2D0m403v1b
Screenshot of how notification is displayed after pulling down on it to expand: http://cl.ly/image/1N1i0G121i2Y
Below is the code snippet I'm using to generate the notification:
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());
回答1:
To use NotificationCompat.MediaStyle
, you must use the android.support.v7.app.NotificationCompat.Builder
- the default v4 Builder does not handle the v7 MediaStyle
来源:https://stackoverflow.com/questions/33810667/mediastyle-notification-to-show-button-in-compact-view