如何在将setOngoing(true)后取消持续通知?

3

这是我用来在Android中创建通知的代码。这个通知按预期显示,但无法通过滑动通知来删除它。 那么我该如何以编程方式将其删除?

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID)
                    .setSmallIcon(R.drawable.small)
                    .setContentTitle("persistent")
                    .setContentText("cant remove").setOngoing(true);
    mBuilder.build();

可能是重复的问题:如何在Android中以编程方式从通知栏中删除通知? - Cătălin Florescu
1个回答

4
在这个答案中,有一个很好的解释如何实现它:https://dev59.com/MmIk5IYBdhLWcg3wdd0l#19268653 在您的情况下应该像这样:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.CHANNEL_ID);

此外,如原回答所述: NotificationManager

1
谢谢。我已经用notificationManager.notify(id, mBuilder.build());发送了通知,并使用您提供的代码notificationManager.cancel(id);将其删除。 - Shobhit Kumar
1
很高兴能够帮忙! :D - Archison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接