144

私はこのコードを介してBroadcastReceiver内に通知を作成しています:

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = R.drawable.ic_stat_notification;
        CharSequence tickerText = "New Notification";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        long[] vibrate = {0,100,200,200,200,200};
        notification.vibrate = vibrate;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        CharSequence contentTitle = "Title";
        CharSequence contentText = "Text";
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        int mynotification_id = 1;

        mNotificationManager.notify(mynotification_id, notification);

通知をクリックすると、NotificationActivityが開き、アクティビティ内でIntent-Bundleからfoo_idを取得できます(例:1)

ただし、別の通知がトリガーされてもう一度クリックすると、アクティビティはIntent-Bundleから「古い」値(1)を受け取ります。clear()を使用してバンドルをクリアしようとしましたが、同じ効果が得られます。sthは私のコードで間違っていると思います。

4

6 に答える 6

283

保留中のインテンスに対して同じリクエストコードを送信しています。これを変える:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

に:

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

同じパラメータを送信した場合、インテントは作成されません。それらは再利用されます。

于 2011-09-10T08:06:29.847 に答える
144

または、次のコードを使用して、PendingIntentを生成することもできます。

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

のドキュメントからPendingIntent.FLAG_UPDATE_CURRENT

説明されているPendingIntentがすでに存在する場合は、それを保持しますが、その余分なデータをこの新しいIntentにあるものに置き換えます。これは、エクストラのみが変更されるインテントを作成していて、以前のPendingIntentを受け取ったエンティティが、明示的に指定されていなくても、新しいエクストラでそれを起動できることを気にしない場合に使用できます。

于 2012-02-17T14:38:39.143 に答える
43

同じIDを渡しています。このような状況では、次のような時間から一意のIDを作成します。

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

そしてそれをこのように置きます:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);
于 2012-08-17T06:29:06.463 に答える
9

久しぶりに最善のアプローチを探している人は、以下に示すように、最後の引数としてPendingIntent.FLAG_UPDATE_CURRENTを渡す必要があります。

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

新しい一意のIDを提供する必要もありません。

初めてではなく、次回以降にこれを行う必要があります

于 2017-03-11T12:29:04.593 に答える
0

すべての通知のリクエストコードは0です。次の行を変更します。

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

と:

PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt(), notificationIntent, 0);
于 2018-02-04T09:03:14.757 に答える
0

別のオプションを追加したかっただけです

 PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
于 2019-08-29T23:18:28.523 に答える