1
这是我的发送通知方法,但问题是我收到通知但无法取消它。我希望我的通知在15分钟后被删除,并且在通知后由用户点击后也会被删除。NotificationManager.cancel(id)不起作用
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
contentIntent = PendingIntent.getActivity(
this,
0,
intent,
0);
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Recite Dua").setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
alamNotificationBuilder.setSound(alarmSound);
long[] pattern = {500, 500, 500, 500, 500, 500, 500, 500, 500};
alamNotificationBuilder.setVibrate(pattern);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
removeNotification();
}
private void removeNotification() {
Handler handler = new Handler();
long delayInMilliseconds = 1000;
handler.postDelayed(new Runnable() {
public void run() {
stopForeground(false);
alarmNotificationManager.cancel(1);
}
}, delayInMilliseconds);
}
使用'PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT)'关闭它,一旦用户点击它。 –
仍然通知仍然存在,我试过FLAG_ONE_SHOT –