0

I want to create a notification for my music player. I want to use two different custom notifications, one for notification area and another for lockscreen. I used setPublicVersion in notification builder for lock screen but it doesn't work.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "media");

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);

remoteViews.setOnClickPendingIntent(R.id.play, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
            PlaybackStateCompat.ACTION_PLAY_PAUSE));

    remoteViews.setOnClickPendingIntent(R.id.pause, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
            PlaybackStateCompat.ACTION_PLAY_PAUSE));

    builder
    .setCustomContentView(remoteViews)
    .setContentIntent(contentPendingIntent)
    .setSmallIcon(R.drawable.logo)
    .setVisibility(VISIBILITY_PRIVATE)
    .setPublicVersion(getLockScreenBuilder(state))
            .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), PlaybackStateCompat.ACTION_STOP));

startForeground(10, builder.build());

// this is a notification for lock screen

private Notification getLockScreenBuilder(PlaybackStateCompat state) {

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "media");


    PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, getPlayerIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_lock_screen);
    remoteViews.setImageViewBitmap(R.id.track_name, textAsBitmap(currentTrack.getTitle(), Typeface.createFromAsset(getAssets(), "iran_sans_mobile_fa_num_bold.ttf"), 45));
    remoteViews.setImageViewBitmap(R.id.channel_title, textAsBitmap(channelTitle, Typeface.createFromAsset(getAssets(), "iran_sans_mobile_fa_num_medium.ttf"), 40));

    if (state.getState() == PlaybackStateCompat.STATE_PLAYING) {

        remoteViews.setViewVisibility(R.id.pause, VISIBLE);
        remoteViews.setViewVisibility(R.id.play, GONE);
    } else {
        remoteViews.setViewVisibility(R.id.pause, GONE);
        remoteViews.setViewVisibility(R.id.play, VISIBLE);
    }

    remoteViews.setOnClickPendingIntent(R.id.play, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
            PlaybackStateCompat.ACTION_PLAY_PAUSE));

    remoteViews.setOnClickPendingIntent(R.id.pause, MediaButtonReceiver.buildMediaButtonPendingIntent(this,
            PlaybackStateCompat.ACTION_PLAY_PAUSE));

    remoteViews.setOnClickPendingIntent(R.id.previous, MediaButtonReceiver.buildMediaButtonPendingIntent
            (this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS));

    remoteViews.setOnClickPendingIntent(R.id.next, MediaButtonReceiver.buildMediaButtonPendingIntent
            (this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT));


    Glide.with(getApplicationContext()).load(Url.ROOT_URL + "storage/podcasts/" + currentTrack.getCoverImage()).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget<Bitmap>(100, 100) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            remoteViews.setImageViewBitmap(R.id.image, resource);
        }
    });

    builder.setCustomContentView(remoteViews)
            .setContentIntent(contentPendingIntent)
            .setSmallIcon(R.drawable.logo)
            .setVisibility(VISIBILITY_PUBLIC)
            .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), PlaybackStateCompat.ACTION_STOP));

    return builder.build();
}

my problem is that lock screen notification is the same as custom view that used for notification area and custom view for lock screen notification doesn't work

Fahime Ghasemi
  • 775
  • 9
  • 13

1 Answers1

0

AFAIK, Showing a different notification layout on the lock-screen only is a feature that does not exists!

However, there might be some ideas or workarounds to achieve this, like Listening or observing screen on/off events to update/replace your notification.

You might be able to achieve this by setting a broadcast receiver that can detect these 3 actions:

<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />

and add your code should checks if screen is locked or not using the KeyguardManager, here is a good example.

Atef Hares
  • 4,715
  • 3
  • 29
  • 61