2016-08-30 23 views
10

Tôi đang làm việc trên một ứng dụng Android. Điều này cuối cùng làm cho việc sử dụng một thông báo với một cái nhìn tùy chỉnh được hiển thị trên màn hình khóa. Thật không may, tôi không thể có được hiệu ứng gợn sóng và độ cao khi tôi chạm vào nó như các thông báo khác. Ngoài ra, một lần chạm duy nhất kích hoạt ý định tôi đã định cấu hình trong khi các thông báo khác yêu cầu nhấn đúp.Chế độ xem thông báo màn hình khóa Android thông báo với Ripple và Nhấn đúp

Tôi đã đưa một ví dụ dự án tối thiểu trên Github:

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

Ví dụ ứng dụng cung cấp hai nút để xuất bản thông báo: một sử dụng một giao diện tùy chỉnh và bị các vấn đề nêu trên và thông báo khác mà sử dụng chế độ xem hệ thống mặc định với hành vi mong muốn.

enter image description here

Bất kỳ ý tưởng về làm thế nào để có được những gợn và độ cao ảnh hưởng mà còn là hành vi chạm đúp (bằng cách giữ quan điểm tùy chỉnh) được chào đón.

PS: Tôi nhắm mục tiêu API 19+ và tôi muốn sử dụng bố cục chế độ xem tùy chỉnh cho thông báo, cùng với setOnClickPendingIntent vì chỉ người nghe này mới có thể mở hoạt động bất kể chế độ bảo mật của thiết bị là gì.

Trả lời

3

Remove setOnClickPendingIntent từ phương pháp publishNotificationWithCustomView và thêm setContentIntent đến xây dựng thông báo:

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

Sau đó tháo android:clickable="true" từ lock_screen_notification.xmllock_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"> 

    .... 
+2

Cảm ơn bạn đã gợi ý. Thật không may, nếu tôi sử dụng _setContentIntent_ thay vì _setOnClickPendingIntent_, khi thiết bị được bảo mật bằng lược đồ, ghim, v.v., mục đích yêu cầu mở khóa màn hình khóa để xem hoạt động. Khi _setOnClickPendingIntent_ được đặt, hoạt động sẽ mở ra mà không cần mở khóa, bất kể chế độ bảo mật là gì. Vì lý do này, đề xuất của bạn không hợp lệ với tôi. – Laurent

+0

@Laurent Bạn đã tìm thấy giải pháp chưa? – cristianomad

Các vấn đề liên quan