gpt4 book ai didi

java - NotificationManager.cancel(id) 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:47 31 4
gpt4 key购买 nike

这是我的发送通知方法,但问题是我收到通知但无法取消它。我希望我的通知在 15 分钟后以及用户点击通知后被删除。

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

最佳答案

使用stopForeground(true);它清除 startForeground 方法中使用的 id 的通知。

关于java - NotificationManager.cancel(id) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44048968/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com