androidnotificationsandroid-notificationsandroid-notification-barnotificationmanager

How to show notification that is added as new each time it is fired from broadcast receiver in Android


I am developing an Android app. In my app, I am showing notification that is fired from inside of a broadcast receiver. Showing notification from receiver is working fine. But I am having a little problem with what I want it to be. It is a self-cancel notification. Cannot be closed by user.

What is happening now is when it is fired, it does not append as new notification in notification bar if one is already existing. Just existing one is keep showing. What I want is I want it appended whenever it is fired no matter how many notifications made from that notification manager inside receiver exist.

This is my receiver that fire self-close notification

public class GalleryImageUploadReceiver extends BroadcastReceiver {

    private Context context;
    private NotificationCompat.Builder notification;
    private static final int NOTIFICATION_ID = 1;
    private NotificationManager nm;

    @Override
    public void onReceive(Context pContext, Intent intent) {
        this.context = pContext;
        initialize(intent);
        showProcessNotification();
        uploadImagesToServer();
    }

    private void initialize(Intent intent)
    {
       //initialize data
    }

    private void uploadImagesToServer()
    {
        // do async task and close itself
        if(nm!=null)
        {
            nm.cancel(NOTIFICATION_ID);
        }
    }

    public void showProcessNotification()
    {
        try{

            notification = new NotificationCompat.Builder(context);
            notification.setAutoCancel(true);
            notification.setSmallIcon(R.mipmap.ic_launcher);
            notification.setWhen(System.currentTimeMillis());
            notification.setTicker("Uploading image(s)...");
            notification.setContentTitle(context.getResources().getString(R.string.app_name));
            notification.setContentTitle("Uploading image(s)...");
            notification.setAutoCancel(true);
            notification.setOngoing(true);
            notification.setDefaults(Notification.FLAG_ONGOING_EVENT);
            nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID,notification.build());
        }
        catch (Exception e)
        {

        }


    }


    public void uploadImages(Context context,Intent intent)
    {
        onReceive(context,intent);
    } 
}

This is how I am firing notification using receiver in activity

//inside a click listener
GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver();
receiver.uploadImages(getBaseContext(),intent);

The problem with that code is, when I click button, new notification overrides to the existing one and so only one notification is displayed in the screen. No matter how many times I click it. For example, if notification last 5 seconds, when I click the button again 2 seconds after first one is fired, that existing one will last 7 seconds. New one is not appended in the notification bar. How can I make it to append as new one whenever the button is clicked.


Solution

  • Use a unique notification id for each notification. Because you are using the constant 'NOTIFICATION_ID' new notifications override the existing ones.