【问题标题】:NotificationManager.cancel(id) not workingNotificationManager.cancel(id) 不工作
【发布时间】:2017-05-18 13:21:48
【问题描述】:

这是我的发送通知方法,但问题是我收到通知但无法取消它。我希望我的通知在 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);
}

【问题讨论】:

  • 用户点击后使用PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT)关闭它。
  • 通知仍然存在,我尝试了 FLAG_ONE_SHOT

标签: java android notifications


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    相关资源
    最近更新 更多