我需要多次创建多个通知。应该出现通知的时间被提取到event_id等等。通知的时间在另一个类中设置。 以下代码会发生什么情况,例如,对于在10:00设置的通知,10:00之后设置的所有通知也会同时出现。请帮忙。只需要显示正确的通知。不是未来的。
for(int j=0;j<event_id.size();j++)
{
if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);//alarm manager
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.friendi_main_logo, event_desc.get(j).toString(), System.currentTimeMillis());
Intent mainScreenIntent=new Intent(getApplicationContext(),MainScreenActivity.class);
mainScreenIntent.putExtra("UserID", user_id);
int uniqueCode=0;
uniqueCode= Integer.parseInt(event_id.get(j).toString());//unique code for each pending intent
//separate pending intent for each alarm.. one alarm manager can invoke only one PI
PendingIntent ListOfNotification=PendingIntent.getActivity(getApplicationContext(), uniqueCode,mainScreenIntent,0);
note.flags=Notification.FLAG_AUTO_CANCEL;
alarmManager.set(AlarmManager.RTC_WAKEUP, Long.valueOf(event_time.get(j).toString()), ListOfNotification);//invokes pending intent @ the event_time
note.setLatestEventInfo(getApplicationContext(), "Event: "+event_title.get(j).toString(), event_group.get(j).toString()+": "+event_desc.get(j).toString(),ListOfNotification );
// Uri path=Uri.parse("android.resource://" + getPackageName() + "/alarm_sms.mp3");
// note.sound=path;
note.defaults=Notification.DEFAULT_ALL;
notificationManager.notify(EVENT_NOTIFY_ID, note);
EVENT_NOTIFY_ID++;
flag=true;
}
}
答案 0 :(得分:0)
所以..你做的是......
if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
{
在这一行之后..
看到没有再次输入if循环... 比如像..
把
j=event_id.size();
这只会出现一个通知..
答案 1 :(得分:0)
尝试使用布尔值仅检查第一个触发器。每当你准备开火第二个时,都要真实。
你处于for循环中,并且每次都在调用notificationManager.notify(EVENT_NOTIFY_ID, note);
,因此所有通知都会被触发。
boolean fire_only_one=true;
for(int j=0;j<event_id.size();j++)
{
if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);//alarm manager
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.friendi_main_logo, event_desc.get(j).toString(), System.currentTimeMillis());
Intent mainScreenIntent=new Intent(getApplicationContext(),MainScreenActivity.class);
mainScreenIntent.putExtra("UserID", user_id);
int uniqueCode=0;
uniqueCode= Integer.parseInt(event_id.get(j).toString());//unique code for each pending intent
//separate pending intent for each alarm.. one alarm manager can invoke only one PI
PendingIntent ListOfNotification=PendingIntent.getActivity(getApplicationContext(), uniqueCode,mainScreenIntent,0);
note.flags=Notification.FLAG_AUTO_CANCEL;
alarmManager.set(AlarmManager.RTC_WAKEUP, Long.valueOf(event_time.get(j).toString()), ListOfNotification);//invokes pending intent @ the event_time
note.setLatestEventInfo(getApplicationContext(), "Event: "+event_title.get(j).toString(), event_group.get(j).toString()+": "+event_desc.get(j).toString(),ListOfNotification );
// Uri path=Uri.parse("android.resource://" + getPackageName() + "/alarm_sms.mp3");
// note.sound=path;
note.defaults=Notification.DEFAULT_ALL;
if(fire_only_one){
notificationManager.notify(EVENT_NOTIFY_ID, note);
fire_only_one=false;
}
EVENT_NOTIFY_ID++;
flag=true;
}
}
fire_only_one=true;
此外,您可以通过在此处仅设置最相关的通知来更改逻辑,当用户通过单击第一个通知打开活动时,使用ALARM管理器设置第二个通知。