16

I'm creating an app that has to show more than one notification. I was looking for information and I use this code in class:

Notification noti = new NotificationCompat.Builder(this)

I can show more than one notifications on the bar, but the problem is that when I click on it, it only shows the first one. I think it's because the id that the pending intent obtain is always the same, but I send a different one.

Notification Activity: Show a new notification when I click the button.

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;

public class NotificationActivity extends Activity {
int notificationID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification_example);
}

public void onClick(View v){
    displayNotification();
}

protected void displayNotification(){
    Intent i = new Intent(this, NotificationView.class);
    i.putExtra("notificationID", notificationID);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    CharSequence ticker ="Display";
    CharSequence contentTitle = "Title";
    CharSequence contentText = "Description";
    Notification noti = new NotificationCompat.Builder(this)
                        .setContentIntent(pendingIntent)
                        .setTicker(ticker)
                        .setContentTitle(contentTitle)
                        .setContentText(contentText)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .addAction(R.drawable.ic_launcher, ticker, pendingIntent)
                        .setVibrate(new long[] {100, 250, 100, 500})
                        .build();
    Log.d("Send Notification", "id: "+notificationID);
    nm.notify(notificationID, noti);
    notificationID++;
}
}

As you can see i increase the notificationID every time i click the button for send a different id.

Notification View: Shows the id of the notification.

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;

public class NotificationView extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);
    txt= (TextView) findViewById(R.id.txt1);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    txt.setText("Notification ID: "+getIntent().getExtras().getInt("notificationID"));
    // Cancel notification
    nm.cancel(getIntent().getExtras().getInt("notificationID"));        
}
}

Hhere only shows the id of the notification, but it's always 1.

Any idea about what happend? I'm testing it on a 2.3 device.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Fernando
  • 349
  • 2
  • 6
  • 21

2 Answers2

33

just put in order to solve the issue:

int num = (int) System.currentTimeMillis();

PendingIntent resultPendingIntent=PendingIntent.getActivity(this,num,resultIntent,Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

mNotificationManager.notify(num, mNotifyBuilder.build());*
ylun.ca
  • 2,504
  • 7
  • 26
  • 47
elias
  • 331
  • 3
  • 2
24
1) PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, i, 0); 
  ..............Your code ........................  
    nm.notify(1, noti); 
2)PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, i, 0);  
  ..............Your code ........................  
    nm2.notify(2, noti);  
3)PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 3, i, 0);  
  ..............Your code ........................  
    nm3.notify(3, noti); 
4)................same as above  
5)................same as above 
AnilPatel
  • 2,356
  • 1
  • 24
  • 40
  • 1
    can you explain it please? i don't understand what you try to tell me. --------------------- Ok, it works :) thanks. – Fernando May 31 '13 at 11:21
  • This code create different different notify but not overwrite – AnilPatel May 31 '13 at 11:26
  • 1
    basically give each pendingintent a unique number here. after using that number in notify() increment it for the next notification that comes in. – Adam Johns May 01 '14 at 18:36