android - 调用 addAction 时删除通知?

标签 android notifications

我在我的通知中添加了两个操作按钮,当我单击它们中的任何一个时,它们会执行所需的操作,但通知仍保留在我的通知抽屉中。我知道单击操作按钮时可以从通知抽屉中删除通知,因为这就是 Gmail 的工作方式。如果我单击主通知,它会打开应用程序并从通知抽屉中删除通知。

这是我的代码 fragment :

Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
        completeIntent.putExtra("rowid", inrowid);
        completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Title")
                .setContentText("text")
                .setContentIntent(notificationReleaseIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .setAutoCancel(true)
                .addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);

编辑 - 正如 zionpi 指出的那样,我需要调用 notification.cancel();单击 addAction 按钮后删除通知。我只是将此方法添加到 PendingIntent 指向的类中。

public static void CancelNotification(Context ctx, int notifyId) {
    String  s = Context.NOTIFICATION_SERVICE;
    NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
    mNM.cancel(notifyId);
}

最佳答案

所以你要去掉你的通知,前提是要有一个通知id。
然后调用类似于下面的方法来消除它。

 public static void CancelNotification(Context ctx, int notifyId) {
        String  s = Context.NOTIFICATION_SERVICE;
        NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
        mNM.cancel(notifyId);
    }

您可能需要引用 thisthis交替发帖。

关于android - 调用 addAction 时删除通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18335037/

相关文章:

ios - 在 iOS 收件箱样式通知中分组或捆绑通知

ios - 收到推送通知后发起网络请求

Android 前台服务通知替换

ruby-on-rails - rails 4 : Notifications by user

android - 如何使用闹钟管理器设置 android 通知的标题?

android - 如何在Android中实例化AudioFormat类?

javascript - 将异常写入包裹

java - Android Firebase 检查数据是否等于字符串

android - 如何在应用程序中访问 c​​om.android.internal.R.bool?

php - 同时向多个用户广播通知 - PHP/MySql/Ajax