【问题标题】:builder.setAutoCancel(true) not workingbuilder.setAutoCancel(true) 不工作
【发布时间】:2016-06-17 05:39:54
【问题描述】:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    final Intent intent = new Intent(MainActivity.this, AlertActivity.class);
    final PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
            builder.setContentText("Click here");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle(getString(R.string.app_name));
            builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent);
            builder.setAutoCancel(true);
            NotificationManagerCompat.from(MainActivity.this).notify(0, builder.build());
        }
    });
}

我还尝试删除 builder.addAction(),但是当我单击通知时,没有任何效果。当点击通知而不添加操作时,我们如何将用户引导到特定活动?此外,无论哪种情况;除非我手动滑动以将其删除,否则我无法关闭通知。

【问题讨论】:

    标签: android android-layout push-notification android-notifications


    【解决方案1】:

    autoCancel 仅在您使用按钮时调用默认通知单击时才有效,您必须自己清除通知

    在您的 NotificationManager 实例上调用 clear(int id) 在您的案例 id 为 0

    【讨论】:

    • 还展示了如何以及在何处使用notification.clear(id)
    • 即使我直接在 onCreate();仍然 autoCancel 不起作用。如何调用 clear()?
    • 当你在通知管理器上调用 notify 时,你给了它一个 id - 这是你以后可以用来访问它的唯一 id(这是来自通知管理器:notify(int id, Notification notification)要取消,您可以调用: cancel(int id) 并使用相同的 id。因此,基本上,您需要跟踪 id 或可能将 id 放入 Bundle 中。
    • 我仍然不明白我应该如何以及在哪里使用它?
    • 这个答案不完整。我编辑了它,完成了它并格式化了它;但编辑尚未被接受。
    【解决方案2】:

    使用setContentIntent 应该可以解决您的问题:

    .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
    

    完整代码:

    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
                builder.setContentText("Click here");
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle(getString(R.string.app_name));
                builder.setAutoCancel(true)
                builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent);
                builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
    NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
            }
        });
    

    更新:

    builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    

    【讨论】:

    • setContentIntent() 有效,谢谢。但是当我们添加了一个动作并点击了该动作时,如何以完全相同的方式关闭通知?
    • 它不工作。 builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    猜你喜欢
    • 2016-04-09
    • 2019-05-02
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2016-02-16
    相关资源
    最近更新 更多