2017-06-23 33 views
0

已解決,謝謝大家的貢獻。幾秒鐘後通知不會被清除。 -Android

這是我用來向用戶顯示通知的函數。但是,由於某種原因,它看起來像通知不會被刪除了......不知道爲什麼。請注意,變量通知被初始化爲:

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

回答

0

或許檢查,這也

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

更新的答案請參閱我使用本地最終變量通知ID –

+0

這很好,謝謝。 – dfabiano

0

好像你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 */); 

insted的調用到另一個Runnable是永遠不會被調用的。

對於清除所有通知使用這種方法:

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

希望它可以幫助你..

+0

送出2次通知,只有刪除1 – dfabiano

+0

你是否在通知中使用相同的密鑰即NOTIFICATION = 1? –

+0

正如你可以在代碼的末尾看到的,通知在函數的末尾增加 – dfabiano

0

爲您的通知值遞增它不會刪除通知,並在延遲後,將檢查的可運行更新的價值。

嘗試使用notificationManager.cancel(NOTIFICATION - 1); 它應該工作

您可以在一個變量NOTIFICATION_DELETED和處理程序儲存最後刪除的價值,

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

它的工作原理,但如果我發送2個通知,它只會刪除第一個通知 – dfabiano

+0

有兩個選項可以清除通知,您可以使用notificationManager.cancelAll()清除所有通知,或者您必須使用循環清除所有通知一一通知。 –

+0

問題是,我不想刪除它們中的所有,只有特定的一個,因爲刪除的時間將會不同,我應該使用與處理程序不同的方法嗎? – dfabiano