2013-04-16 22 views
10

tôi tìm thấy nhiều câu trả lời cho điều này, nhưng không ai giúp đỡ :( tôi có mã này:thông báo loại bỏ Android (tạo ra bởi mNotifyBuilder) trên nhấp chuột

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.icon; 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    int notifyID = 96; 
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context) 
     .setContentTitle("MyApp") 
     .setContentText(message) 
     .setDefaults(Notification.DEFAULT_ALL) 
     .setAutoCancel(true) 
     .setSmallIcon(icon); 

    Notification notification = mNotifyBuilder.build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    mNotificationManager.notify(notifyID, notification); 
} 

Nhưng nếu tôi bấm vào thông báo không có gì xảy ra và nó vẫn là . có Trong tài liệu hướng dẫn là, mà tôi cần phải sử dụng:

.setAutoCancel(true) 

một số một có vấn đề tương tự và ai đó nói với anh ta sử dụng:

notification.flags |= Notification.FLAG_AUTO_CANCEL; 

Tôi sử dụng cả hai, nhưng không có kết quả: ( Cảm ơn bạn rất nhiều vì đã trả lời. :)

Trả lời

19

Tôi nghĩ rằng nếu bạn không sử dụng Intent với thông báo của bạn, cách duy nhất để bỏ nó để swipe nó.

Nếu không, bạn có thể sử dụng Intent để mở hoạt động của bạn và điều đó thực sự sẽ xóa thông báo:

Intent showIntent = new Intent(this, YourActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0); 

Và để thêm nó vào thông báo:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context) 
    .setContentTitle("MyApp") 
    .setContentText(message) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setAutoCancel(true) 
    .setContentIntent(contentIntent) 
    .setSmallIcon(icon); 

Hope this helps!

+4

Tôi làm việc! Tôi chỉ cần thực hiện một chút thay đổi: \t \t \t .setContentIntent (PendingIntent.getActivity (HandymanApplication.getContext(), 0, \t \t \t \t \t ý định, PendingIntent.FLAG_CANCEL_CURRENT)) – user1696947

+2

bình luận thực sự đã giúp tôi nhiều –

+0

vấn đề Th là bảng thông báo được tự động ẩn khi bạn nhấn vào thông báo của bạn. Dường như việc đạt được hành vi tương tự như thể bạn đã xóa thông báo bằng cách vuốt nó là điều không thể. –

8

Người dùng có thể loại bỏ tất cả thông báo hoặc nếu bạn đặt thông báo của mình thành tự động hủy thông báo cũng sẽ bị xóa khi người dùng chọn nó.

Bạn cũng có thể gọi hủy() cho một ID thông báo cụ thể trên NotificationManager. Cuộc gọi phương thức cancelAll() xóa tất cả các thông báo bạn đã phát hành trước đó.

Ví dụ:

mNotificationManager.cancel(notifyId); 
+0

tôi cần xóa thông báo sau khi người dùng nhấp. Tôi tìm thấy một giải pháp :) – user1696947

0

chỉ cần sử dụng dòng này nút bên onclick event-

mNotificationManager.cancel(notifyId);//id is that u used for notification 
0

chỉ cần thêm

Intent showIntent = new Intent(); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0); 


    myNotification = new Notification.Builder(getApplicationContext()) 
        .setContentTitle("Title") 
        .setContentText("Text") 
        .setTicker("notificatio") 
        .setContentIntent(contentIntent) 
        .setDefaults(Notification.DEFAULT_ALL) 
        .setAutoCancel(true) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .build(); 

thêm ý định cấp phát rỗng sau đó trên nhấp chuột vào thông báo, thông báo sẽ loại bỏ. nó làm việc cho tôi.

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