0

私はこのように私のアプリケーションでローカル通知を使用しています。

    showNotification(this, "Title1", "Message One", 1);
    showNotification(this, "Title2", "Message Two", 2);
    showNotification(this, "Title3", "Message Three", 3);
    showNotification(this, "Title4", "Message Four", 4);


public static void showNotification(Context con, String title,
        String message, int id) {


    NotificationManager manager = (NotificationManager) con
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification note = new Notification(R.drawable.ic_noti_logo,title, System.currentTimeMillis());

    Intent notificationIntent = new Intent(con,Result.class);
    notificationIntent.putExtra("Message", message);
    notificationIntent.putExtra("NotiId", id);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(con, 0,
            notificationIntent, PendingIntent.FLAG_ONE_SHOT);

    note.setLatestEventInfo(con, title, message, pi);

    note.defaults |= Notification.DEFAULT_ALL;
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    manager.notify(id, note);
}

Resut.javaで

    message = getIntent().getStringExtra("Message");
    notiId = getIntent().getIntExtra("NotiId", 0);

    showAlert(message,notiId);

private void showAlert(String msg, int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                //  finish();
                    cancelNotification(Result.this,id);
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

public static void cancelNotification(Context con, int id) {

    NotificationManager manager = (NotificationManager) con
            .getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(id);
}

私の問題は、通知バーに4つの通知メッセージが表示され、それらのいずれかをクリックするとResultアクティビティにリダイレクトされますが、2回目にクリックしても効果がなかった場合にのみ発生します。私を助けてください。

4

1 に答える 1

1

問題は、4つの通知がPendingIntent同等のインテントを参照しているため、同じものを共有していることです(Intent.filterEquals()のドキュメントでは、インテントは、アクション、データ、クラス、タイプ、またはカテゴリのいずれかで異なる必要があると説明されています。インテントが等しいかどうかを判断する際には、特に考慮されません)。さらに、使用しているのは1回だけ使用できることPendingIntent.FLAG_ONE_SHOTを保証します。PendingIntent

于 2012-12-27T21:08:47.297 に答える