2013-04-23 10 views
18

ここでは他のすべてのAUTO-CANCEL-not-workingの質問を見てきましたが、それらはすべて私がしていない間違いを含んでいるようです。私は両方のAndroid:アプリがバックグラウンドで実行されているときに通知にAUTO-CANCELを使用する

builder.setAutoCancel(true); 

Notification notif = builder.build(); 
notif.flags |= Notification.FLAG_AUTO_CANCEL; 

どちらの作品を試してみました。

私の最小APIは8であるため、NotificationCompatを使用しています。ここに私の完全なコードがあります。この特定の通知では、ユーザーは何もする必要はないので、私は意図を呼んでいません。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentTitle(getString(R.string.app_name) + ": my title"); 
builder.setContentText(message); 
builder.setSmallIcon(R.drawable.notification_icon); 

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prog_icon); 
builder.setLargeIcon(bitmap); 

builder.setAutoCancel(true); // dismiss notification on user click 

NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
notiManager.notify(MY_NOTI_MANAGER_ID, builder.build()); 

通知は完全に表示されます。あなたはそれをクリアするためにスワイプすることができます。しかし、単にそれをタップしても、通知は却下されません。それだけで点灯し、そこにとどまる。

私のコードと他の人との間にいくつかの違いがあります。ここに掲載されています: 1)私はNotificationCompatを使用しています(違いはありませんが、これまで聞いたことがあります)。 2)通知が簡単なので、私は意図を添付しません。

洞察があれば教えてください。

編集:私の目的は、私のバックグラウンドアプリを取り囲むことなく通知を却下することです。

答えて

24

は保留中の意図を必要とします。

Android - notification manager, having a notification without an intentで、私は(あなたが通知を解任するために、独自の活動を開始する必要がないように)あなたの保留中の意図として、現在アクティブなアプリケーションをつかむ解決策を見つけました。

私は(右の自動キャンセルを設定した後)、次の2つのコード行を追加:それは素晴らしい仕事

PendingIntent notifyPIntent = 
    PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);  
builder.setContentIntent(notifyPIntent); 

を。ユーザーが通知をクリックした結果としてアクティビティを再開したくない場合は、これが最善の選択肢だと言います。

+0

これは正しいです... – superUser

+2

解決策は良いですが、説明が不正確だと思います。 「現在のアクティブなアプリケーションを保留中の意図として取得する」ことはありません。空のインテントを送信し、通知トレイを折りたたむ可能性が高くなります。意図は何もしないので、私たちはアクティビティスタックの先頭に戻ります。 –

+0

ああ、チャンピオンのように働いて..ありがとう.. –

6

PendingIntentsetContentIntent()コールが欠落しているようです。私はそれが働くために自動キャンセルに必要と考えています。

ここではいくつかのNotification作品this sample projectからロジック-displayingされていますので、どうやらあなたはを行う

private void raiseNotification(Intent inbound, File output, Exception e) { 
    NotificationCompat.Builder b=new NotificationCompat.Builder(this); 

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL) 
    .setWhen(System.currentTimeMillis()); 

    if (e == null) { 
     b.setContentTitle(getString(R.string.download_complete)) 
     .setContentText(getString(R.string.fun)) 
     .setSmallIcon(android.R.drawable.stat_sys_download_done) 
     .setTicker(getString(R.string.download_complete)); 

     Intent outbound=new Intent(Intent.ACTION_VIEW); 

     outbound.setDataAndType(Uri.fromFile(output), inbound.getType()); 

     b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0)); 
    } 
    else { 
     b.setContentTitle(getString(R.string.exception)) 
     .setContentText(e.getMessage()) 
     .setSmallIcon(android.R.drawable.stat_notify_error) 
     .setTicker(getString(R.string.exception)); 
    } 

    NotificationManager mgr= 
     (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    mgr.notify(NOTIFY_ID, b.build()); 
    } 
+1

ありがとうございます。私はちょうどこの解決策を見つけました: PendingIntent notifyPendingIntent = PendingIntent.getActivity(getApplicationContext()、0、新しいIntent()、0); \t \t builder.setContentIntent(notifyPendingIntent); このソリューションはここに掲載されました:http:// stackoverflow。com/questions/7040742/android-notification-manager-of-not-intent – BeccaP

4

あなたが特定の 時間後に(音楽プレーヤーのように)これを使用することができる必要がある場合、キャンセル可能でないことを表示するには、 通知(ユーザーにはキャンセルできません)。

mNotificationBuilder .setSmallIcon(android.R.drawable.btn_plus); 
mNotificationBuilder .setContentTitle("My notification"); 
mNotificationBuilder .setContentText("Notificattion From service"); 
mNotificationBuilder .setLights(0xFF0000FF, 500, 500); 

Notification note = mNotificationBuilder.build(); 
note.flags = Notification.FLAG_ONGOING_EVENT; // For Non cancellable notification 
mNotificationManager.notify(NOTIFICATION_ID, note); 
関連する問題