2010-05-15 20 views
10

से अधिसूचना आइकन निकालें मैं स्टेटस बार में एक आइकन प्रदर्शित कर रहा हूं.अब मैं उस सामग्री को खोलने के तुरंत बाद उस आइकन को हटाना चाहता हूं, अगर हमें कोई चेतावनी मिलती है, तो वह आइकन फिर से प्रदर्शित होगा। मैं यह कैसे कर सकता हूँ?स्टेटस बार

उत्तर

34

अपनी अधिसूचना रद्द करने के लिए अधिसूचना प्रबंधक का उपयोग करें। आपको केवल अपनी अधिसूचना आईडी प्रदान करने की आवश्यकता है।

https://developer.android.com/reference/android/app/NotificationManager.html

private static final int MY_NOTIFICATION_ID= 1234; 
String ns = Context.NOTIFICATION_SERVICE; 
NotificationManager mNotificationManager; 
mNotificationManager = (NotificationManager) getSystemService(ns); 
mNotificationManager.notify(MY_NOTIFICATION_ID, notification); 

उदाहरण कोड पूरा नहीं हुआ है। यह इस बात पर निर्भर करता है कि आपने अपनी अधिसूचना कैसे बनाई है। बस सुनिश्चित करें कि आप अपनी सूचना को रद्द करने के लिए उसी आईडी का उपयोग करते हैं, जब आपने अपनी सूचना बनाई थी।

रद्द करने के लिए:

mNotificationManager.cancel(MY_NOTIFICATION_ID); 
15

आप एक बार उपयोगकर्ता उस पर क्लिक किया, अधिसूचना झंडा FLAG_AUTO_CANCEL सेट इससे पहले कि आप सूचना बनाएं अधिसूचना निकालना चाहते हैं।

1

मैंने बिल्डर पॉटर का उपयोग किया ताकि आप बसटर setAutoCancel(true) से स्वत: रद्द कर सकें। ऐसा कुछ ऐसा दिखता है:

String title = "Requests"; 
    String msg = "New requests available."; 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_gcm_icon) 
        .setContentTitle(title) 
        .setAutoCancel(true) 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
-1
Intent resultIntent = new Intent(application, MainActivity.class); 
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(application, 0, resultIntent, 0); 
NotificationManager nmgr = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE); 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(application) 
      .setSmallIcon(R.drawable.icon_battery) 
      .setContentTitle(application.getString(R.string.app_name)) 
      .setContentText("your text") 
      .setOnlyAlertOnce(false) 
      .setAutoCancel(true) 
      .setTicker("your ticker") 
      .setDefaults(Notification.DEFAULT_SOUND ) //| Notification.DEFAULT_VIBRATE 
      .setContentIntent(resultPendingIntent) 
      .setVisibility(VISIBILITY_SECRET) 
      .setPriority(Notification.PRIORITY_MIN); 

Notification mNotification = mBuilder.build(); 
// mNotification.flags |= FLAG_NO_CLEAR; 
nmgr.notify(0, mNotification); 
संबंधित मुद्दे