2013-02-27 7 views
93

通知を閉じるには、ユーザーがクリックした後に終了します。誰もがフラグを使用すると言っているのを見ましたが、通知クラスではなく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()); 

答えて

1

あなたの通知にフラグを追加することができます:

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

これは、クリックすると、それを却下します
は、ここに私の私は、通知を設定していたコードです。

+0

ここでフラグを追加しますか?私のコードで私を見せてもらえますか? –

+0

通知オブジェクトの通知フラグに追加します。 – JoxTraex

258

簡単に、単純にこれを呼び出す:

mBuilder.setAutoCancel(true); 

をまた、それはあなたが本当にFLAG_AUTO_CANCELを使用したい場合は、mNotificationManager.notifyを呼び出す前に、これだけを呼び出し、本当に必要はありませんしながら、:

mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 
+2

これは最高の答えです... flag_auto_cancelは機能していませんでした。 – allemattio

+1

後のgetNotificationメソッドは廃止されました!!! – sandalone

+0

最初の作品です!感謝万円! – Vladimir

15

これを試してみてください....

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()); 
+1

'.getNotification()'は廃止されました。 '.build()'を使用してください。代わりに 'mBuilder.build()。flags。= Notification.FLAG_AUTO_CANCEL;'と同じです。 –