Multiple notification handling

4.3k views Asked by At

I'm having trouble in figuring out the Intent, PendingIntent Filters and Flags for notifications.

Notification are working and are getting generated as they should, but the problem is only the last notification created keeps the Bundle Data.

I want all notification to keep the Bundle Data of each notification in it until a user click on them.

Consider an application for a moment when different user send you a message a new notification gets created and when you click on any notification the app launches and takes you to some specific Activity. I want the same thing but when there are more than one notification the last notification keeps the Data where as previous notification loose their Bundle Data and Intent.

There is another thing which Filters to use to restrict the app from launching a new instance of MainActivity everytime a notification is clicked at.

The Notification_ID are different for each notification.

public class AlarmSchedulingService extends IntentService {
private NotificationManager mNotificationManager;
public AlarmSchedulingService() {
    super("SchedulingService");
}

protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        sendNotification(extras.getInt(KEY_EXTRAS_NOTIFICATION_ID));
}

public void sendNotification(int NOTIFICATION_ID) {
mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(keyName, extraData); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent,  PendingIntent.FLAG_UPDATE_CURRENT);

    // use the right class it should be called from the where alarms are set
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(titleString)
            .setStyle(
                    new NotificationCompat.BigTextStyle()
                            .bigText(messageString))
            .setDefaults(
                Notification.DEFAULT_SOUND
                | Notification.DEFAULT_LIGHTS)

            .setContentText(messageString);
        mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
     }
 }
1

There are 1 answers

1
Apurva Kolapkar On BEST ANSWER

This is Showing that :

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent,  PendingIntent.FLAG_UPDATE_CURRENT);

You are giving request code 0 to all notifications. 0 should be replaced by each unique number, otherwise, each new notification will override the old one.