android notification works for one object in a loop [duplicate]

喜你入骨 提交于 2019-12-25 07:39:35

问题


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;
    }

}

回答1:


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:

Do:

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.




回答2:


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());



回答3:


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.



来源:https://stackoverflow.com/questions/16881045/android-notification-works-for-one-object-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!