如何无意中消除通知? [英] How to dismiss notification without intent?

查看:51
本文介绍了如何无意中消除通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序中显示一条通知,当点击该通知而不启动活动时,该通知会消失.

I want to display a notification inside the app that disappears when the notification is tapped without starting an activity.

我使用一个空的意图,并且有效:

I use an empty intent and it works:

Intent intent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "")
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(title)
                .setContentText(text)
                .setAutoCancel(true)
                .setTicker(text);
                .setContentIntent(contentIntent);

但是,发生了一些崩溃:

However, there are some crashes:

java.lang.RuntimeException: bad array lengths
    at android.os.Parcel.readIntArray(Parcel.java:789)
    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:339)
    at android.app.NotificationManager.notify(NotificationManager.java:139)
    at android.app.NotificationManager.notify(NotificationManager.java:112)
    ...

根据此SO答案,崩溃似乎是由于 intent 没有启动活动.

According to this SO answer the crashes seem to happen because intent is not starting an activity.

可以通过其ID消除通知,但是当没有活动可启动并调用dismiss方法时,我无法将其消除.

A notification can be dismissed by its ID, but I cannot dismiss it when there is no activity to start and call the dismiss method.

如何在不使用空意图的情况下轻按应用内的轻击通知?

How can I dismiss the notification on tap inside the app without using an empty intent?

根据此问题这似乎是一个Android问题.我报告的崩溃也发生在Android 4.3上.

According to this SO questions it seems to be an Android issue. The crashes I got reported also happened on Android 4.3.

根据此SO答案,这似乎是由于使用 setLargeIcon设置的位图过大代码>.因此,现在我要使用Glide减小位图的大小.

According to this SO answer it seems to be due to a too large bitmap set with setLargeIcon. So I am reducing the bitmap size now with Glide.

推荐答案

您可以创建一个调用 BroadcastReceiver 的意图,然后取消该通知.

You can create an intent which calls a BroadcastReceiver which then cancels the notification.

您的 PendingIntent 可以这样创建:

private PendingIntent getCancelNotificationIntent() {
    Intent cancelIntent = new Intent(context, CancelNotification.class);
    return PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

CancelNotification.java 类类似于以下内容:

public class CancelNotification extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        notificationManager.cancel(NOTIFICATION_ID);
    }
}


NOTIFICATION_ID 是您定义的常量,并按如下所示传递给 NotificationManager :


NOTIFICATION_ID is a constant you define and pass to the NotificationManager like this:

notificationManager.notify(NOTIFICATION_ID, yourNotificationObject);

注意:不要忘了像下面这样在您的清单文件中注册接收者:

NOTE: don't forget to register the receiver in your manifest file like this:

<receiver android:name="com.example.package.CancelNotification" />

这篇关于如何无意中消除通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆