Android的通知适用于一个对象在一个循环 [英] android notification works for one object in a loop
问题描述
我有一个使用Web服务连接到服务器的机器人服务,该服务返回许多JSON对象,我要为每个对象运行的通知后,我做了这一切,但我有问题,就是我刚才看到一个通知,即使以为服务器发送两个对象。请看为
循环,很明显,我打电话通知很多,为什么我刚才看到一个显示?
公共类GetOffersService延伸服务{ @覆盖
公共无效的onCreate(){
super.onCreate();
Client客户端=新的客户端(HTTP://+ Configuration.hostIP
+:8080 / TEST2 / SSSSSS /);
字符串str = client.getBaseURI(报价);
尝试{
JSONArray JSON =新JSONArray(STR);
的for(int i = 0; I< json.length();我++){
JSONObject的oneOffer = json.getJSONObject(I)
INT OFFERID = oneOffer.getInt(ID);
串offerDescriptoin = oneOffer.getString(描述);
字符串结束日期= oneOffer.getString(结束日期);
字符串的startDate = oneOffer.getString(起始日期);
JSONObject的餐厅= oneOffer.getJSONObject(餐厅);
INT restaruantID = restaurant.getInt(ID);
字符串restaurantName = restaurant.getString(名称);
意向意图=新意图(这一点,OfferNotification.class);
的PendingIntent pIntent = PendingIntent.getActivity(这一点,0,
意图,0);
乌里soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
这一点).setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher,呼,pIntent)
.addAction(R.drawable.ic_launcher,多,pIntent)
.addAction(R.drawable.ic_launcher,添加更多,pIntent)
.setContentTitle(以下简称Eattel)
.setContentText(新发售!)setSound(soundUri)。
//创建一个明确的意图在你的应用的活动
意图resultIntent =新意图(这一点,OfferNotification.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(本); 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());
}
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
} @覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
返回null;
}}
这是从<一个href=\"http://developer.android.com/reference/android/app/NotificationManager.html#notify%28java.lang.String,%20int,%20android.app.Notification%29\"相对=nofollow>文档:
公共无效通知(字符串标记,INT ID,通知通告)
发布通知,在状态栏中显示。 如果通知
用相同的ID已发布的应用程序,并有
尚未被取消时,将通过更新后的信息替换。强>
块引用>在粗体部分解释了为什么看到你的情况下,只有一个通知
所以,这听起来可能是:
mNotificationManager.notify(100,mBuilder.build());
在你的code中的缺陷。
一些不同的ID对每个项目替换100。
mNotificationManager.notify(someUniqueId,mBuilder.build());
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; } }
解决方案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());
这篇关于Android的通知适用于一个对象在一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!