已解決,謝謝大家的貢獻。幾秒鐘後通知不會被清除。 -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++;
}
更新的答案請參閱我使用本地最終變量通知ID –
這很好,謝謝。 – dfabiano