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.

Laurent
  • 14,122
  • 13
  • 57
  • 89

1 Answers1

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

    ....
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • 2
    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. – Laurent Sep 05 '16 at 16:11
  • @Laurent Have you find a solution? – cristianomad Feb 23 '18 at 16:47
  • Still no solutions :X so boring – Laurent Aug 14 '18 at 16:06