Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12685

No effect on notification click in Android

I am using a local notification in my application like this.

    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);
}

in 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);
}

my problem is that, I am getting 4 notification message in notification bar and when I am clicking any of them I am redirecting to Result Activity but it hapens only ones when second time I clicked there was no effect. please help me.

Upvotes: 0

Views: 335

Answers (1)

dsandler
dsandler

Reputation: 2481

The problem is that the four notifications share the same PendingIntent because they reference equivalent Intents (the docs for Intent.filterEquals() explain that to be considered distinct, Intents must differ in either action, data, class, type, or categories—note that extras are specifically not considered when determining whether Intents are equal). Furthermore, you're using PendingIntent.FLAG_ONE_SHOT which guarantees the PendingIntent can only be used once.

Upvotes: 1

Related Questions