1

FirstActivity から 3 つの個別の通知を作成し、それらをクリックすると、同じアプリケーションにある SecondActivity を開始します。問題は、SecondActivity の文字列の値が常に私が投稿した最後の通知であることです。SecondActivity を開始する必要があり、id = 1 の通知をクリックすると、通知 1 から呼び出しが表示され、id = 2 および id =3 の場合も同様です。しかし、インテント オブジェクトに入れられたエクストラが失われています。以前にそのような状況に遭遇したことがある場合は、これを手伝ってください。PendingIntent フラグ FLAG_CANCEL_CURRENT を試しましたが、無駄でした。前もって感謝します。:-)

NotificationManager nm;
Notification n;
PendingIntent pi1, pi2, pi3;
Button btnNotify1, btnNotify2, btnNotify3;
Intent intent;

final static String TICKER_TEXT = "My_Notification";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    btnNotify1 = (Button) findViewById(R.id.btnNotify1);
    btnNotify2 = (Button) findViewById(R.id.btnNotify2);
    btnNotify3 = (Button) findViewById(R.id.btnNotify3);

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

    n = new Notification(R.drawable.ic_launcher, TICKER_TEXT,
        System.currentTimeMillis());
    n.flags = n.flags | Notification.FLAG_AUTO_CANCEL;

    intent = new Intent(this, SecondActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    btnNotify1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            intent.putExtra("call_is_from", "Notification 1");
            pi1 = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
            n.setLatestEventInfo(getApplicationContext(), "Notification 1",
                "One", pi1);
            nm.notify(1, n);

        }
    });
    btnNotify2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            intent.putExtra("call_is_from", "Notification 2");
            pi2 = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
            n.setLatestEventInfo(getApplicationContext(), "Notification 2",
                "Two", pi2);
            nm.notify(2, n);

        }
    });
    btnNotify3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            intent.putExtra("call_is_from", "Notification 3");
            pi3 = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
            n.setLatestEventInfo(getApplicationContext(), "Notification 3",
                "Three", pi3);
            nm.notify(3, n);

        }
    });
}
4

1 に答える 1

2

のそれぞれが一意であることを確認する必要がありますPendingIntentPendingIntent同じものを使用して同じものを取得しているため、常に同じものを取得しますIntent(エクストラが異なっていても)。requestCodePendingIntent

PendingIntentそれぞれの が一意であることを確認するには、次のrequestCodeように、それぞれに異なる を使用します。

pi1 = PendingIntent.getActivity(getApplicationContext(), 1,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

pi2 = PendingIntent.getActivity(getApplicationContext(), 2,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

pi3 = PendingIntent.getActivity(getApplicationContext(), 3,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
于 2013-07-15T12:39:02.137 に答える