【问题标题】:Remove notification after clicking点击后删除通知
【发布时间】:2019-04-15 18:12:46
【问题描述】:

我希望在用户点击通知后关闭通知。我看到每个人都说要使用标志,但我在任何地方都找不到标志,因为我使用的是 NotificationCompat.Builder 类而不是 Notification 类。有人知道如何让她自己删除通知吗?
这是我设置通知时的代码:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Question")
            .setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");

    Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(openHomePageActivity);

    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);       

    mNotificationManager.notify(0, mBuilder.build());

【问题讨论】:

    标签: android eclipse notifications


    【解决方案1】:

    简单,只需调用这个:

    mBuilder.setAutoCancel(true);
    

    另外,虽然不是真的有必要,但如果你真的想使用FLAG_AUTO_CANCEL,请在调用mNotificationManager.notify 之前调用它:

    mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    

    【讨论】:

    • 这是最好的答案..flag_auto_cancel 不起作用..你拯救了我的一天!
    • getNotifications 已弃用,请改用:mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    • mBuilder.build() 也已弃用
    • 当我点击通知时,这个 setAutoCancel(true) 确实有效。但是当我点击通知中的操作(在我的情况下是调用)时,它不起作用!
    • 请在此处查看我的问题:stackoverflow.com/questions/37595594/…
    【解决方案2】:

    试试这个....

    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
     ..........
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.push_notify_icon)
                .setContentTitle("New Question!")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");
    mBuilder.setContentIntent(contentIntent);
    
        ..............        
    
    
    mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(0, mBuilder.build());
    

    【讨论】:

    • .getNotification() 已弃用,现在使用 .build() 代替 mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    【解决方案3】:

    这里是通知:

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_calendar)
                .setContentTitle("My Firebase Push notification")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
    

    点击取消的关键是:

                .setAutoCancel(true)
    

    我希望它能解决问题。

    【讨论】:

    • 我在前台显示通知,当用户点击.setAutoCancel(true) 时我想关闭该通知已经使用它,但对前台通知不起作用。
    【解决方案4】:

    您可以在通知中添加标志:

    http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

    这将在单击时将其关闭。

    【讨论】:

    • 将其添加到您的 Notification 对象的通知标志中。
    【解决方案5】:

    在 kotlin 中,您可以使用:

    mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多