NotificationManager.cancel(id)无法正常工作

时间:2017-05-18 13:21:48

标签: java android notifications

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

答案 0 :(得分:0)

使用startForeground 它会清除import subprocess output = subprocess.check_output('ping localhost', stderr=subprocess.STDOUT, shell=True) 方法中使用id的通知。