已解决,感谢所有贡献的人。
这是我用来向用户显示通知的功能。但是,由于某种原因,看起来通知不会被删除...不知道为什么。请注意,变量NOTIFICATION初始化为:
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 :(得分: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 */);
将其称为另一个永远不会被调用的Runnable
。
要清除所有通知,请使用以下方法:
public void clearNotifications(Context context) {
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
希望对你有帮助..
答案 1 :(得分:0)
当您的通知值增加时,它不会删除通知,并且在延迟后的Runnable中,它将检查更新的值。
尝试使用notificationManager.cancel(NOTIFICATION - 1);
它应该工作
您可以将最后删除的值存储在一个变量NOTIFICATION_DELETED和处理程序
中for(int i = NOTIFICATION_DELETED ;i < NOTIFICATION;i++){
notificationManager.cancel(i);
}
答案 2 :(得分: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++;
}