How to add button to notifications in android?

前端 未结 8 1465
孤独总比滥情好
孤独总比滥情好 2020-12-05 03:53

My app plays music and when users open notifications screen by swiping from the top of the screen ( or generally from the bottom right of the screen on tablets ), I want to

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 04:18

    I think that beside Ankit Gupta answer, you can use MediaSession (API > 21) to add native mediaController view :

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                .setShowActionsInCompactView(new int[]{playPauseButtonPosition})  // show only play/pause in compact view
                .setMediaSession(mSessionToken))
            .setColor(mNotificationColor)
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setUsesChronometer(true)
            .setContentIntent(createContentIntent(description)) // Create an intent that would open the UI when user clicks the notification
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(art);
    

    Source: tutorial

    you can alse create custom view and display it in the notificcation area , first answer here is great.

提交回复
热议问题