Android Lock Screen Notification Custom View with Ripple and Double Tap
Asked Answered
W

1

10

I am working on an Android app. This last makes use of a notification with a custom view that is displayed on the lock screen. Unfortunately, I am not able to get the ripple and elevation effect when I tap on it like other notifications. Also, a single touch trigger the intent I have configured whereas other notifications require double tap.

I have put a minimal project example on Github:

https://github.com/lpellegr/android-notification-custom-example

The app example offers two buttons to publish notifications: one that uses a custom view and suffer from the issues mentioned above and another notification that uses the default system view with the expected behaviour.

enter image description here

Any idea about how to get the ripple and elevation effect but also the double tap behaviour (by keeping the custom view) is welcome.

PS: I am targeting API 19+ and I want to use a custom view layout for the notification, along with setOnClickPendingIntent since only this listener allows to open an activity whatever the security mode of the device is.

Whomp answered 30/8, 2016 at 16:33 Comment(0)
O
3

Remove setOnClickPendingIntent from the method publishNotificationWithCustomView and add setContentIntent to the notification builder:

private void publishNotificationWithCustomView() {
    String title = "Notification Custom View";
    String content = "No ripple effect, no elevation, single tap trigger";
    Context context = getApplicationContext();

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(DEFAULT_ALL)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setOnlyAlertOnce(true)
                    .setAutoCancel(false)
                    .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                    .setContentTitle(title)
                    .setContentText(content)
                    .setOngoing(true)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(createLockscreenNotificationPendingIntent(context));

    int notificationLayoutResId = R.layout.lock_screen_notification;

    // using folder layout-vX is having issue with LG devices
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        notificationLayoutResId = R.layout.lock_screen_notification_android_n;
    }

    RemoteViews remoteView = new RemoteViews(
            context.getPackageName(), notificationLayoutResId);
    remoteView.setTextViewText(R.id.title, title);
    remoteView.setTextViewText(R.id.text, content);

    builder.setCustomContentView(remoteView);

    Notification notification = builder.build();
    publishNotification(context, notification, 7);
}

Then remove android:clickable="true" from lock_screen_notification.xml and lock_screen_notification_android_n.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="64dp">

    ....
Ovida answered 5/9, 2016 at 8:56 Comment(3)
Thank you for the suggestion. Unfortunately, if I use setContentIntent instead of setOnClickPendingIntent, when the device is secured with a schema, pin, etc. the intent requires to unlock the lock screen to see the activity. When setOnClickPendingIntent is set, the activity opens without unlocking, whatever the security mode is. For this reason, your suggestion is not valid to me.Whomp
@Whomp Have you find a solution?Stringpiece
Still no solutions :X so boringWhomp

© 2022 - 2024 — McMap. All rights reserved.