Skip to content Skip to sidebar Skip to footer

Showing Multiple Notification Messages With Single Id

I have several events which happen at the same time. I need to display multiple notification messages to user in serial way. The ideal case is, each notification message will take

Solution 1:

If you need only one Notification in the status bar, but you want the message to indicate all of your app's notifications, then you need to manage the single Notification by changing the message.

Every time you repost the Notification with sendNotification(message), the Notification ticker text will be updated.

For example, if you have a single notification of an item in your hypothetical inbox, you can post a message like this:

Message from Joe.

When you receive multiple new messages, change it to:

You have (n) messages.

When the user presses the Notification, you should redirect to an Activity that displays everything in something like a ListView or GridView, to display all of your messages.


Solution 2:

I create my own message queuing system. Not using single id, but able to achieve what I want

only 1 message will be shown, if several notification requests were made at the same time. every message will take turn to show up. no message lost.

this.blockingQueue.offer(message);

Here is the complete message queuing system.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    executor.submit(new NotificationTask());
}

private class NotificationTask implements Runnable {
    @Override
    public void run() {
        while (!executor.isShutdown()) {
            try {
                String message = blockingQueue.take();
                notification(message);
                // Allow 5 seconds for every message.
                Thread.sleep(Constants.NOTIFICATION_MESSAGE_LIFE_TIME);
            } catch (InterruptedException ex) {
                Log.e(TAG, "", ex);
                // Error occurs. Stop immediately.
                break;
            }                
        }

    }
}

@Override
public void onDestroy() {
    // Will be triggered during back button pressed.
    super.onDestroy();
    executor.shutdownNow();
    try {
        executor.awaitTermination(100, TimeUnit.DAYS);
    } catch (InterruptedException ex) {
        Log.e(TAG, "", ex);
    }
}

private void notification(String message) {
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this.getActivity())
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(this.getString(R.string.app_name))
        .setTicker(message)
        .setContentText(message)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true);

    // Do we need to have sound effect?

    final NotificationManager mNotificationManager =
        (NotificationManager) this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    final int id = atomicInteger.getAndIncrement();
    mNotificationManager.notify(id, mBuilder.build());

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new NotificationManagerCancelRunnable(mNotificationManager, id), Constants.NOTIFICATION_MESSAGE_LIFE_TIME);
}

// Use static class, to avoid memory leakage.
private static class NotificationManagerCancelRunnable implements Runnable {
    private final NotificationManager notificationManager;
    private final int id;

    public NotificationManagerCancelRunnable(NotificationManager notificationManager, int id) {
        this.notificationManager = notificationManager;
        this.id = id;
    }

    @Override
    public void run() {
        notificationManager.cancel(id);
    }
}

// For notification usage. 128 is just a magic number.
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(128);
private final AtomicInteger atomicInteger = new AtomicInteger(0);    

Post a Comment for "Showing Multiple Notification Messages With Single Id"