有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NotificationManager。取消(id)不工作

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

共 (1) 个答案

  1. # 1 楼答案

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