当前位置: 动力学知识库 > 问答 > 编程问答 >

NotificationBuilder.addAction().autoCancel() not work in Android 6.0

问题描述:

I have an notification in my app and this notification has button that was set using addAction()method. But every time that user click in the button of the notification the notification don't auto cancel unless that user clicked explicitly in the notification.

private static void notification(Context context, String title, String text, int id, Intent intent, int priority, boolean withSound) {

PendingIntent pendingIntent = PendingIntent.getActivity(context, Constants.REQUEST_CODE_NOTIFICATION, intent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);

notificationBuilder.setSmallIcon(R.drawable.ic_stat_notify);

notificationBuilder.setContentTitle(title);

notificationBuilder.setContentText(text);

notificationBuilder.setColor(ContextCompat.getColor(context, R.color.bg_primary));

notificationBuilder.setAutoCancel(true);

notificationBuilder.setContentIntent(pendingIntent);

notificationBuilder.setPriority(priority);

if (Constants.NOTIFICATION_NEW_AD_BALANCE == id)

notificationBuilder.setTicker(text);

if (Constants.NOTIFICATION_DATE_TIME_SETTINGS == id)

notificationBuilder.addAction(0, context.getString(R.string.notification_device_time_settings), pendingIntent).setAutoCancel(true);

notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text));

if (withSound) {

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

notificationBuilder.setSound(alarmSound);

notificationBuilder.setVibrate(new long[]{500, 500});

} else {

notificationBuilder.setVibrate(new long[0]);

}

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(id, notificationBuilder.build());

}

网友答案:

To solve this problem I have to start my notification using the following pending intent:

pendingIntent = PendingIntent.getBroadcast(context, Constants.REQUEST_CODE_NOTIFICATION, intent, PendingIntent.FLAG_CANCEL_CURRENT);

After I have to implement a broadcastreceiver to be triggered when user perform the click on notification button how can you see below:

public class DateTimeSettingsBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startActivity(new Intent(Settings.ACTION_DATE_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        NotificationBuilder.cancel(context, Constants.NOTIFICATION_DATE_TIME_SETTINGS);
    }
}

This way the problem of notification not being dismissed after user perform click was solved.

网友答案:

What did you expect from setAutoCancel()? Reading the docs, it says

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.

It sounds like your code is working correctly. I believe you want the previous notification to dismiss when a new one is added? I can't remember for sure, but I think if you use the same id, it will replace the current one. Otherwise, I know you can cancel the notification with it's id:

notificationManager.cancel(id);
分享给朋友:
您可能感兴趣的文章:
随机阅读: