刪除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.xml
和lock_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">
....
謝謝你的建議。不幸的是,如果我使用_setContentIntent_而不是_setOnClickPendingIntent_,那麼當設備使用模式,引腳等進行保護時,意圖要求解鎖鎖定屏幕以查看活動。當設置_setOnClickPendingIntent_時,無論安全模式是什麼,該活動都將打開而不解鎖。出於這個原因,你的建議對我無效。 – Laurent
@Laurent你有沒有找到解決方案? – cristianomad