0

I have an android service which connects to a server using web service, the service return many json objects, I want to run notification for each object, I did all of that but I have problem which is i just see one notification even thought the server is sending two objects. please look at the for loop,it is obvious that i am calling many notifications, why I just see one is shown?

public class GetOffersService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        Client client = new Client("http://" + Configuration.hostIP
                + ":8080/test2/ssssss/");
        String str = client.getBaseURI("offers");
        try {
            JSONArray json = new JSONArray(str);
            for (int i = 0; i < json.length(); i++) {
                JSONObject oneOffer = json.getJSONObject(i);
                int offerID = oneOffer.getInt("ID");
                String offerDescriptoin = oneOffer.getString("Description");
                String endDate = oneOffer.getString("EndDate");
                String startDate = oneOffer.getString("StartDate");
                JSONObject restaurant = oneOffer.getJSONObject("Restaurant");
                int restaruantID = restaurant.getInt("ID");
                String restaurantName = restaurant.getString("Name");
                Intent intent = new Intent(this, OfferNotification.class);
                PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                        intent, 0);
                Uri soundUri = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        this).setSmallIcon(R.drawable.ic_launcher)
                        .addAction(R.drawable.ic_launcher, "call", pIntent)
                        .addAction(R.drawable.ic_launcher, "more", pIntent)
                        .addAction(R.drawable.ic_launcher, "add more", pIntent)
                        .setContentTitle("The Eattel")
                        .setContentText("New Offer!").setSound(soundUri);
                // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(this, OfferNotification.class);

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

                stackBuilder.addParentStack(OfferNotification.class);

                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder
                        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Signin.NOTIFICATION_SERVICE);

                mNotificationManager.notify(100, mBuilder.build());
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
user2387331
  • 99
  • 1
  • 3
  • 11

3 Answers3

3

Please read the "stack notification" chapter here: http://developer.android.com/design/patterns/notifications.html

If your app creates a notification while another of the same type is still pending, avoid creating an altogether new notification object. Instead, stack the notification.

A stacked notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.

Don't:

enter image description here

Do:

enter image description here

You can provide more detail about the individual notifications that make up a stack by using the expanded digest layout. This allows users to gain a better sense of which notifications are pending and if they are interesting enough to be read in detail within the associated app.

enter image description here

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
3

This is from the documentation:

public void notify (String tag, int id, Notification notification) Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

The part in bold explains why see only one notification in your case

So, it sound likely that:

 mNotificationManager.notify(100, mBuilder.build());

Is the flaw in your code.

replace 100 by some different id for each item.

 mNotificationManager.notify(someUniqueId, mBuilder.build());
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • you said to me bad words, I didn't say anything, bty, i can't even downvote. you said `shame one me` and you said `tell me to not download your app`, you said bad hurt words – user2387331 Jun 02 '13 at 09:15
  • An not taking in account advice that I took time to format, link to official documentation, put illustration pictures, reply to comments just to get ignored hurts more.. And this was done before even posting the other answer.. BTW, it's the second time I give you the answer: use an other notification id. what should I do to highlight this answer???? – Waza_Be Jun 02 '13 at 09:17
0

If you want not to follow the guidelines, that's up to you but that's very sad. Please think twice and consider my previous answer.

Please at least read the documentation:

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

So, it sound likely that:

 mNotificationManager.notify(100, mBuilder.build());

Is the flaw in your code.

replace 100 by some different id for each item.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260