2017-06-23 2 views
0

Gelöst, danke an alle, die beigetragen haben.Benachrichtigung wird nach einigen Sekunden nicht gelöscht. -Android

Dies ist meine Funktion, mit der ich die Benachrichtigung für den Benutzer anzeigen kann. Aus irgendeinem Grund sieht es jedoch so aus, als ob die Benachrichtigungen nicht mehr gelöscht werden ... nicht sicher warum. Beachten Sie die Variable MELDUNG initialisiert als:

public static int NOTIFICATION = 1; 
public void displayNotification(String title, String message, Intent intent) { 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(mCtx, NOTIFICATION, 
      intent, PendingIntent.FLAG_UPDATE_CURRENT); //FLAG_ONE_SHOT ? FLAG_UPDATE_CURRRENT 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx); 
    Notification notification; 
    notification = mBuilder.setSmallIcon(R.mipmap.ic_logo_nobg).setTicker(title).setWhen(0) 
      .setContentIntent(resultPendingIntent) 
      .setContentTitle(title) 
      .setSmallIcon(R.mipmap.ic_logo_nobg) 
      .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_roundedlogo)) 
      .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
      .setPriority(NotificationCompat.PRIORITY_HIGH) 
      .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    final NotificationManager notificationManager = (NotificationManager) mCtx 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(NOTIFICATION, notification); 

    // This method will get rid of the notification AND the message after 1 day 
    Handler handler = new Handler(Looper.getMainLooper()); 
    handler.post(new Runnable() { 
     public void run() { 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        notificationManager.cancel(NOTIFICATION); 
        Preference_Manager.getInstance(mCtx).deleteKeyMessage(NOTIFICATION); 
        // Preference_Manager.getInstance(mCtx).deleteKeyMessageid(NOTIFICATION); 
       } 
      }, 5000/*howMany */); 

     } 
    }); 
    NOTIFICATION++; 
} 

Antwort

0

Vielleicht

public static int NOTIFICATION = 1; 


public void displayNotification(String title, String message, Intent intent) { 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(mCtx, NOTIFICATION, 
      intent, PendingIntent.FLAG_UPDATE_CURRENT); //FLAG_ONE_SHOT ? FLAG_UPDATE_CURRRENT 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx); 
    Notification notification; 
    notification = mBuilder.setSmallIcon(R.mipmap.ic_logo_nobg).setTicker(title).setWhen(0) 
      .setContentIntent(resultPendingIntent) 
      .setContentTitle(title) 
      .setSmallIcon(R.mipmap.ic_logo_nobg) 
      .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_roundedlogo)) 
      .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
      .setPriority(NotificationCompat.PRIORITY_HIGH) 
      .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    final NotificationManager notificationManager = (NotificationManager) mCtx 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(NOTIFICATION, notification); 

    final int notificationId = NOTIFICATION; 

    // This method will get rid of the notification AND the message after 1 day 
    Handler handler = new Handler(Looper.getMainLooper()); 
    handler.post(new Runnable() { 
     public void run() { 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        notificationManager.cancel(notificationId); 
        Preference_Manager.getInstance(mCtx).deleteKeyMessage(notificationId); 
        // Preference_Manager.getInstance(mCtx).deleteKeyMessageid(NOTIFICATION); 
       } 
      }, 5000/*howMany */); 

     } 
    }); 
    NOTIFICATION++; 
} 
+0

Siehe Ich verwende lokale endgültige Variable für Benachrichtigungs-ID –

+0

Dies funktionierte perfekt, danke. – dfabiano

0

Es ist wie Ihr run() Methode scheint nicht aufgerufen. Verwenden Sie diesen Code direkt

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     notificationManager.cancel(NOTIFICATION); 
     Preference_Manager.getInstance(mCtx).deleteKeyMessage(NOTIFICATION); 
     // Preference_Manager.getInstance(mCtx).deleteKeyMessageid(NOTIFICATION); 
    } 
}, 5000/*howMany */); 

insted es in einer anderen Runnable den Aufruf, die nie aufgerufen wird.

Für klare alle Benachrichtigung Verwendung dieser Methode:

public void clearNotifications(Context context) { 
    NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancelAll(); 
} 

Hoffe, dass es Ihnen helfen ..

+0

Sent 2 auch prüfen Benachrichtigungen, nur gelöscht 1 – dfabiano

+0

verwenden Sie den gleichen Schlüssel in beiden Benachrichtigungen dh NOTIFICATION = 1 ?? –

+0

Wie Sie am Ende des Codes sehen können, wird die Benachrichtigung am Ende der Funktion inkrementiert. – dfabiano

0

Es wird nicht entfernen Benachrichtigung als MELDUNG Wert erhöht wird und in Runnable der Post verzögert wird es prüfen Aktualisierter Wert

Versuchen verwenden notificationManager.cancel(NOTIFICATION - 1); Es sollte

arbeiten können Sie letzten Wert in einer Variablen NOTIFICATION_DELETED gelöscht speichern und in Ihrem Handler,

for(int i = NOTIFICATION_DELETED ;i < NOTIFICATION;i++){ 
    notificationManager.cancel(i); 
} 
+0

es funktionierte, aber wenn ich 2 Benachrichtigungen sende, wird es nur die erste löschen – dfabiano

+0

Es gibt zwei Optionen zum Löschen von Benachrichtigungen, Entweder Sie können alle Benachrichtigungen mit notificationManager.cancelAll() löschen, oder Sie müssen die Schleife verwenden, um alle zu löschen Benachrichtigung eins nach dem anderen. –

+0

Die Sache ist, dass ich nicht alle löschen möchte, nur das bestimmte, da die Zeiten für das Löschen anders sein, sollte ich eine andere Methode als ein Handler verwenden? – dfabiano

Verwandte Themen