2

我想在用户选择通知时关闭通知。这是我用来向用户显示通知的代码。我在GCM(谷歌云消息传递)中使用它

private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);
       NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle(" Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg.toString());

        mBuilder.build().flags = Notification.FLAG_AUTO_CANCEL;
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(notificationSound);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

但是,当我选择它正在打开活动但没有关闭通知的通知时,这对我来说效果不佳。请建议我出了什么问题

4

4 回答 4

3

尝试

mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID); // use this to cancel notification

或者你可以试试

mBuilder.setAutoCancel(true); //所以当用户在面板中点击通知时会自动取消通知

于 2013-09-03T09:46:53.510 回答
1

Just remove these lines :

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

And

mBuilder.setContentIntent(contentIntent);
于 2013-09-03T09:45:01.267 回答
1

利用setAutoCancel (boolean autoCancel)

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.

参考: http: //developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setAutoCancel(boolean)

于 2013-09-03T09:46:52.190 回答
0
            Notification notification = new Notification(
                    YOUR_DRAWABLE_RESOURCE, "Message",
                    System.currentTimeMillis());
            notification.flags = Notification.FLAG_AUTO_CANCEL;

选中后,您可以使用FLAG_AUTO_CANCEL取消通知。

于 2013-09-03T09:52:00.693 回答