在点击Android的删除通知(由mNotifyBuilder创建) [英] Android remove notification(created by mNotifyBuilder) on click

查看:146
本文介绍了在点击Android的删除通知(由mNotifyBuilder创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多答案,但没有帮助:( 我有这个code:

i found many answer to this, but none help :( I have this code:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notifyID = 96;
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
        .setContentTitle("MyApp")
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setSmallIcon(icon);

    Notification notification = mNotifyBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(notifyID, notification);
}

但是,如果我点击的通知没有发生,这是仍然存在。在文档,我需要使用:

But if i click on notification nothing happen and it is still there. In documentation is, that i need to use:

.setAutoCancel(true)

有个人也有类似的问题,有人告诉他使用方法:

Some one have similar problem and somebody tells him to use:

notification.flags |= Notification.FLAG_AUTO_CANCEL;

我同时使用,但没有结果:( 非常感谢您的回答。 :)

I use both, but no result :( Thank you very much for answers. :)

推荐答案

我认为,如果你不使用意向与您的通知,只有这样,才能驳回其刷卡吧。

I think that if you don't use an Intent with your notification, the only way to dismiss its to swipe it.

否则,您可以使用一个Intent打开你的活动,这将真正清除通知:

Otherwise you can use an Intent to open your activity and that will actually clear the notification:

Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);

和将其添加到通知:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
    .setContentTitle("MyApp")
    .setContentText(message)
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setContentIntent(contentIntent)
    .setSmallIcon(icon);

希望这有助于!

Hope this helps!

这篇关于在点击Android的删除通知(由mNotifyBuilder创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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