MediaStyle Notification to Show Button in Compact View

断了今生、忘了曾经 提交于 2021-01-27 16:33:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!