130

ユーザーが通知をクリックした後、通知を閉じてほしい。誰もがフラグを使用するように言っているのを見ましたが、NotificationクラスではなくNotificationCompat.Builderクラスを使用しているため、どこにもフラグを見つけることができません。誰かが自分で通知を削除する方法を知っていますか?
通知を設定するときのコードは次のとおりです。

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Question")
            .setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");

    Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(openHomePageActivity);

    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);       

    mNotificationManager.notify(0, mBuilder.build());
4

5 に答える 5

337

簡単です。これを呼び出すだけです。

mBuilder.setAutoCancel(true);

また、本当に必要というわけではありませんが、本当に を使いたい場合はFLAG_AUTO_CANCEL、 を呼び出す前にこれを呼び出してくださいmNotificationManager.notify:

mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
于 2013-03-27T17:00:59.870 に答える
19

これを試して....

NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 ..........
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.push_notify_icon)
            .setContentTitle("New Question!")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");
mBuilder.setContentIntent(contentIntent);

    ..............        


mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, mBuilder.build());
于 2014-08-19T16:10:35.253 に答える
3

通知にフラグを追加できます。

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

これは、クリックすると閉じます。

于 2013-02-27T19:42:52.470 に答える