2012-09-30 33 views
22

क्या एंड्रॉइड में यह पता लगाने के लिए कोई तरीका है कि उपयोगकर्ता बाईं ओर अधिसूचना को स्वाइप करता है और इसे हटा देता है? मैं एक दोहराने वाली चेतावनी सेट करने के लिए अलार्ममैनगर का उपयोग कर रहा हूं और जब उपयोगकर्ता द्वारा अधिसूचना रद्द की जाती है तो मुझे रोकने के लिए मेरी दोहराने वाली चेतावनी की आवश्यकता होती है।यह पता लगाने के लिए कि क्या कोई अधिसूचना खारिज कर दी गई है?

दोहराई जाने वाली चेतावनी की स्थापना:

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

मेरी सूचना कोड:

@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(); 

} 

मैं जानता हूँ कि मैं आदेश मेरी दोहरा अलार्म को रद्द करने में alarmManager.cancel(displayIntent) कॉल करने की आवश्यकता यहाँ मेरी कोड है। हालांकि, मुझे नहीं पता कि यह कोड कहां रखना है। मुझे केवल दोहराना चेतावनी रद्द करने की आवश्यकता है जब उपयोगकर्ता ने अधिसूचना पर टैप किया है या इसे खारिज कर दिया है। आपकी सहायताके लिए धन्यवाद!

+2

कोई बात नहीं, मुझे मेरा जवाब यहां मिला: http://stackoverflow.com/questions/8811876/notification-deleteintent-does-not-work। – NewGradDev

उत्तर

15

मुझे विश्वास है कि Notification.deleteIntent वह है जिसे आप ढूंढ रहे हैं। दस्तावेज़ कहते हैं: जब अधिसूचना स्पष्ट रूप से उपयोगकर्ता द्वारा खारिज कर दिया गया है

मंशा पर अमल करने, या तो "सभी को साफ़ करें" बटन के साथ या व्यक्तिगत रूप से दूर स्वाइप करने से। यह शायद एक गतिविधि शुरू नहीं करना चाहिए क्योंकि उनमें से कई एक ही समय में भेजे जाएंगे।

+7

कैसे? क्या आप कुछ नमूना कोड प्रदान करके मदद कर सकते हैं? –

+0

इस समाधान ने मेरी मदद की: https://stackoverflow.com/a/14723652/563509 – masterwok

0

उन सभी भविष्य के लोगों के लिए - आप अधिसूचना हटाने के लिए सुनने के लिए एक प्रसारण रिसीवर पंजीकृत कर सकते हैं।

एक नया प्रसारण रिसीवर बनाएँ:

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

आपके आवेदन वर्ग के भीतर रजिस्टर प्रसारण रिसीवर:

"आपके ऐप का लक्ष्य एपीआई स्तर 26 या बाद, आप प्रकट उपयोग नहीं कर सकते सबसे निहित प्रसारण के लिए एक रिसीवर घोषित करने के लिए (प्रसारण जो आपके ऐप को विशेष रूप से लक्षित नहीं करते हैं)। "

Android Documentation.

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

आप शायद स्थिर चर Config.NotificationDeleteAction देखा। यह आपकी अधिसूचना के लिए एक अद्वितीय स्ट्रिंग पहचानकर्ता है। यह आम तौर पर निम्नलिखित {namespace} इस प्रकार {actionName} सम्मेलन:

you.application.namespace.NOTIFICATION_DELETE

अपनी अधिसूचना बिल्डर पर हटाने के इरादे सेट करें:

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

कहां, createDeleteIntent निम्न विधि है:

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

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

आपकी पंजीकृत प्रसारण रिसीवर को आपकी सूचना को खारिज करते समय इरादा प्राप्त करना चाहिए।