android通知只显示最后一个通知

时间:2013-06-07 22:39:51

标签: android service android-notifications

我有应用程序android从服务器提供数据,我想发出通知,每行数据一个通知,我想当用户按下通知,活动被触发,我想用android做所有这些服务,我可以做到这一切。

我的问题是,无论用户按哪个通知,它都会显示最后一行数据。

代码:

lient client = new Client(Configuration.getServer());
        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");
                Offer offer = new Offer(offerID, startDate, endDate,
                        offerDescriptoin, new Restaurant(restaruantID,
                                restaurantName));
                Log.d("DES", offerDescriptoin);
                Offer.getAllOffers().put(offer.getID(), offer);
                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("Offer from " + restaurantName)
                        .setContentText(offerDescriptoin).setSound(soundUri);
                // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(this, OfferNotification.class);
                resultIntent.putExtra("offerID", offer.getID());
                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(Context.NOTIFICATION_SERVICE);

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

1 个答案:

答案 0 :(得分:0)

确保在以下电话中   mNotificationManager.notify(offer.getID(),mBuilder.build());

传递的ID是唯一的,否则将替换任何具有匹配ID的待处理通知。

检查文档

http://developer.android.com/reference/android/app/NotificationManager.html