带有涟漪和双击效果的Android锁屏通知自定义视图

10

我正在开发一款Android应用程序。该应用程序使用自定义视图显示在锁屏上的通知。不幸的是,当我点击它时,我无法获得类似于其他通知的涟漪和高程效果。此外,单次触摸即可触发我配置的意图,而其他通知需要双击。

我已经在Github上放了一个最小化的项目示例:

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

该应用程序示例提供了两个按钮来发布通知:一个使用自定义视图,存在上述问题,另一个通知使用默认系统视图,并具有预期的行为。

enter image description here

欢迎任何关于如何获得涟漪和高程效果以及双击行为(保持自定义视图)的想法。

PS:我针对API 19+进行定位,并且希望在通知中使用自定义视图布局,以及setOnClickPendingIntent,因为只有此侦听器才允许在设备的任何安全模式下打开一个活动。

1个回答

3

从方法publishNotificationWithCustomView中删除setOnClickPendingIntent,并将setContentIntent添加到通知构建器中:

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);
}

然后从lock_screen_notification.xmllock_screen_notification_android_n.xml中删除android:clickable="true"

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

    ....

2
谢谢您的建议。不幸的是,如果我使用_setContentIntent_而不是_setOnClickPendingIntent_,当设备被安全模式锁定时,需要解锁锁屏才能查看意图。当设置_setOnClickPendingIntent_时,无论安全模式如何,活动都会在未解锁的情况下打开。因此,您的建议对我无效。 - Laurent
@Laurent 你找到解决方案了吗? - cristianomad
仍然没有解决方案 :X 太无聊了 - Laurent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接