28

ユーザーがタップすると削除される通知を表示しようとしています。クラスを使用しNotificationCompatて通知を作成setAutoCancel(true)し、ビルダーを呼び出します。これはコードの一部です:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

通知は正しく追加されていますが、タップしても何も起こりません! 私は何を間違っていますか?

4

2 に答える 2

57

setContentIntent を使用すると、問題が解決するはずです。

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));

あなたの例では:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

多くの場合、ユーザーを関連するコンテンツに誘導したい場合があるため、「new Intent()」を別のものに置き換えることがあります。

デモを githubにアップロードしました。

于 2013-03-19T16:51:39.857 に答える
30

回答が既に受け入れられていることは知っていますが、別のソリューションで同じ問題が発生したため、ここで共有します。

私にとっては、同じNotificationCompat.Builderオブジェクトを使用して、 を呼び出す通知を作成していましたsetOngoing(true)。これは、作業中に削除してはならないアップロード進行状況通知用でした。

とにかく、タスクが完了した後、私は電話をかけsetAutoCancel(true)ましたが、通知はまだスワイプされていませんでした. 私がしなければならなかったことも電話setOngoing(false)でした。

今ではかなり明白に思えますが、将来誰かの時間を節約できるかもしれません。

于 2014-04-21T21:01:39.077 に答える