0

我有应用程序 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();
        }
4

1 回答 1

0

确保在以下调用 mNotificationManager.notify(offer.getID(), mBuilder.build());

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

也检查文档

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

于 2013-06-07T22:50:24.947 回答