2012-09-30 29 views
22

Có cách nào trong Android để phát hiện khi người dùng vuốt thông báo sang bên trái và xóa thông báo đó không? Tôi đang sử dụng một thiết bị báo động để đặt cảnh báo lặp lại và tôi cần cảnh báo lặp lại của mình để dừng khi thông báo bị người dùng hủy. Dưới đây là mã của tôi:Cách phát hiện xem một thông báo đã bị loại bỏ chưa?

Thiết lập cảnh báo lặp đi lặp lại:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent); 

mã thông báo của tôi:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Get the notification ID. 
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key"); 

    //Get the name of the reminder. 
    String reminderName = getIntent().getExtras().getString("Reminder_Name"); 

    //PendingIntent stores the Activity that should be launched when the user taps the notification. 
    Intent i = new Intent(this, ViewLocalRemindersDetail.class); 
    i.putExtra("NotifID", notifID); 
    i.putExtra("notification_tap", true); 

    //Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered. 
    PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK); 

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis()); 

    CharSequence from = "Here's your reminder:"; 

    CharSequence message = reminderName;   
    notif.setLatestEventInfo(this, from, message, displayIntent); 

    //Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms. 
    notif.defaults |= Notification.DEFAULT_SOUND; 
    notif.vibrate = new long[] { 100, 250, 100, 500 }; 
    nm.notify(notifID, notif); 

    //Destroy the activity/notification. 
    finish(); 

} 

Tôi biết tôi cần phải gọi alarmManager.cancel(displayIntent) để hủy báo động lặp đi lặp lại của tôi. Tuy nhiên, tôi không hiểu nơi để đặt mã này. Tôi chỉ cần hủy thông báo lặp lại khi người dùng đã nhấn vào thông báo hoặc loại bỏ thông báo đó. Cảm ơn bạn đã giúp đỡ!

+2

Không sao, tôi tìm thấy câu trả lời của tôi ở đây: http://stackoverflow.com/questions/8811876/notification-deleteintent-does-not-work. – NewGradDev

Trả lời

15

Tôi tin rằng Notification.deleteIntent là những gì bạn đang tìm kiếm. Tài liệu nói:

Mục đích thực thi khi thông báo được người dùng loại bỏ rõ ràng, bằng nút "Xóa tất cả" hoặc bằng cách vuốt nó đi riêng lẻ. Điều này có lẽ không nên khởi chạy một hoạt động vì một vài trong số đó sẽ được gửi cùng một lúc.

+7

Làm cách nào? Bạn có thể giúp bằng cách cung cấp một số mã mẫu? –

+0

Giải pháp này đã giúp tôi: https://stackoverflow.com/a/14723652/563509 – masterwok

0

Để tất cả những người trong tương lai ra khỏi đó - bạn có thể đăng ký bộ thu phát sóng để nghe thông báo xóa inents.

Tạo một máy thu phát sóng mới:

public class NotificationBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (action == null || !action.equals(Config.NotificationDeleteAction)) { 
      return; 
     } 

     // Do some sweet stuff 
     int x = 1; 
    } 
} 

đăng ký thu phát sóng trong lớp ứng dụng của bạn:

"Nếu mục tiêu ứng dụng của bạn mức API 26 hoặc cao hơn, bạn không thể sử dụng manifest tuyên bố người nhận cho hầu hết các chương trình phát sóng tiềm ẩn (các chương trình phát sóng không nhắm mục tiêu cụ thể đến ứng dụng của bạn). "

Android Documentation.

registerReceiver(
     new NotificationBroadcastReceiver(), 
     new IntentFilter(Config.NotificationDeleteAction) 
); 

Bạn có thể nhận thấy các tĩnh biến Config.NotificationDeleteAction. Đây là số nhận dạng chuỗi duy nhất cho thông báo của bạn. Nó thường theo sau {namespace} {} actionName ước:

you.application.namespace.NOTIFICATION_DELETE

Đặt mục đích xóa trên builder thông báo của bạn:

notificationBuilder 
     ... 
     .setDeleteIntent(createDeleteIntent()) 
     ... 

Ở đâu, createDeleteIntent là phương pháp sau:

private PendingIntent createDeleteIntent() { 
    Intent intent = new Intent(); 
    intent.setAction(Config.NotificationDeleteAction); 

    return PendingIntent.getBroadcast(
      context, 
      0, 
      intent, 
      PendingIntent.FLAG_ONE_SHOT 
    ); 
} 

Người nhận phát sóng đã đăng ký của bạn sẽ nhận được ý định khi thông báo của bạn bị loại bỏ.