Zenogrammer Zenogrammer - 1 year ago 198
Android Question

Generate int unique id as android notification id

When i send multiple of push notifications , i need them to be all shown in notification bar , ordered by the time sent desc , i know i should use unique notification i tried to generate random number but that did not solve my problem since i need them to be ordered , i tried to use atomicint and still dont have the desired result

package com.mypackage.lebadagency;
import java.util.concurrent.atomic.AtomicInteger;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import android.widget.RemoteViews;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMNotificationIntentService extends IntentService {

private AtomicInteger c = new AtomicInteger(0);
public int NOTIFICATION_ID = c.incrementAndGet();

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {

public static final String TAG = "GCMNotificationIntentService";

protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

String messageType = gcm.getMessageType(intent);

if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {

for (int i = 0; i < 3; i++) {
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
} catch (InterruptedException e) {

Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

+ extras.get(Config.MESSAGE_KEY));
Log.i(TAG, "Received: " + extras.toString());

private void sendNotification(String msg) {

Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
//here start
Intent gcmintent = new Intent(this, AppGcmStation.class);
gcmintent.putExtra("ntitle", msg);
int requestID = (int) System.currentTimeMillis();
//here end
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,
gcmintent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
.setContentTitle("my title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
mBuilder.setLights(Color.RED, 3000, 3000);

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");

i need the BEST and simple way to generate int id which is incremental to assign it as notification id

Answer Source

You are using the same notification ID (the value is always 1) for all your notifications. You probably should separate out the notification ID into a separate singleton class:

public class NotificationID {
    private final static AtomicInteger c = new AtomicInteger(0);
    public static int getID() {
        return c.incrementAndGet();

Then use NotificationID.getID() instead of NOTIFICATION_ID in your code.

EDIT: As @racs points out in a comment, the above approach is not enough to ensure proper behavior if your app process happens to be killed. At a minimum, the initial value of the AtomicInteger should be initialized from some activity's saved state rather than starting at 0. If the notification IDs need to be unique across restarts of the app (again, where the app process may be killed off), then the latest value should be saved somewhere (probably to shared prefs) after every increment and restored when the app starts.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download