Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lock Screen Notification Custom View with Ripple and Double Tap

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.

like image 660
Laurent Avatar asked Aug 30 '16 16:08

Laurent


1 Answers

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">

    ....
like image 182
Mattia Maestrini Avatar answered Nov 08 '22 16:11

Mattia Maestrini