PlayerNotificationManager not showing notification for playing video on exoplayer, android?

Viewed 834

My app plays video

I am following this Exoplayer Notifications

Have the same code

 PlayerNotificationManager playerNotificationManager = new PlayerNotificationManager(
            appCMSPresenter.getCurrentContext(),
            "kingkdfn",
            459, new DescriptionAdapter());

    playerNotificationManager.setPlayer(getPlayer());

The DescriptionAdapter

public class DescriptionAdapter implements
    PlayerNotificationManager.MediaDescriptionAdapter {

@Override
public String getCurrentContentTitle(Player player) {
    int window = player.getCurrentWindowIndex();
    return "getTitle(window)";
}

@Nullable
@Override
public String getCurrentContentText(Player player) {
    int window = player.getCurrentWindowIndex();
    return "getDescription(window)";
}

@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player,
                                  PlayerNotificationManager.BitmapCallback callback) {

    return null;
}

@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
    return null;
}
}

All added in manifest

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

But still the notification is not showing when video is playing. Do I need to add anything extra?

1 Answers

Create notification

public void createNotification() {
        if (notificationAdapter == null)
            notificationAdapter = new PlayerNotificationAdapter();
        if (pnm == null) {
            pnm = new PlayerNotificationManager.Builder(mContext, 239, mContext.getString(R.string.player_notification_channel_id))
                    .setChannelNameResourceId(R.string.player_notification_channel_name)
                    .setChannelDescriptionResourceId(R.string.player_notification_channel_desc)
                    .setMediaDescriptionAdapter(notificationAdapter).build();
        }
}

Notification adaptor

class PlayerNotificationAdapter : PlayerNotificationManager.MediaDescriptionAdapter {

override fun getCurrentContentTitle(player: Player): String {
    return "Content Title"
}

override fun createCurrentContentIntent(player: Player): PendingIntent? {
    return null
}

override fun getCurrentContentText(player: Player): String? {
    return "Content Description"
}

override fun getCurrentLargeIcon(player: Player, callback: PlayerNotificationManager.BitmapCallback): Bitmap? {
    return playerImage()
}

fun playerImage(): Bitmap? {
    var image: Bitmap? = null
    try {
        var url = URL("content image url")
        image = BitmapFactory.decodeStream(url.openConnection().getInputStream())
    } catch (e: IOException) {

    }
    return image
}
}

Set player to notification manager

if (pnm != null) {
                    pnm.setUseNextAction(false);
                    pnm.setUsePreviousAction(false);
                    MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, "Player");
                    mediaSession.setActive(true);
                    mediaSession.setMetadata(new MediaMetadataCompat.Builder()
                            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
                            .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, description)
                            .build());
                    MediaSessionConnector mediaSessionConnector =
                            new MediaSessionConnector(mediaSession);
                    mediaSessionConnector.setPlayer(simpleExoPlayer);
                    pnm.setMediaSessionToken(mediaSession.getSessionToken());
                    pnm.setPlayer(simpleExoPlayer);
                    pnm.setPriority(PRIORITY_LOW);
                }
Related