1

从 FirstActivity 我创建了三个单独的通知,并在单击它们时启动位于同一应用程序中的 SecondActivity。问题是 SecondActivity 中字符串的值始终是我发布的最后一个通知。当我单击 id = 1 的通知时,我需要启动 SecondActivity 并显示来自通知 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

您需要确保您的每个PendingIntents 都是独一无二的。由于您得到的是PendingIntentusing sameIntent和 same requestCode,因此您将始终得到相同的结果PendingIntent(即使附加功能不同)。

为确保您的每个PendingIntents 都是唯一的,请为每个使用不同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 回答