-1

I need to create multiple notifications at multiple times. The time when the notification is supposed to appear is fetched into the event_id,etc.time for notification is set in another class. What happens with the code below is, for a notification set at, say, 10:00, all the notifications that are set after 10:00 also appear at the same time. Please help. Only the correct notification needs to appear. Not the future ones.

        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;
                            }
                        }
Sherlock
  • 1
  • 1
  • I think this is causing the problem; `event_time.get(j).toString())>System.currentTimeMillis()`. – Chromium Apr 06 '12 at 06:10
  • @Chromium: but i cant remove that because the if i remove this condition, the all the previously set notifications come up too. I dont want that. – Sherlock Apr 06 '12 at 06:16
  • I don't mean, you remove that. Instead of checking for greater than, can't you check for equality? – Chromium Apr 06 '12 at 07:10

2 Answers2

0

So.. what you do is..

if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
  {

after this line..

see that the if loop is not entered again... for example like this..

put

   j=event_id.size();

this makes only one notification appear..

5hssba
  • 8,079
  • 2
  • 33
  • 35
0

try using a boolean to check only the first one fires. make it true whenever you are ready to fire the 2nd one. you are in a for loop, and you are calling notificationManager.notify(EVENT_NOTIFY_ID, note); every time, so all notifications would be fired.

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;

Also You can change the logic by setting only the most relevant notification here, and when the user opens an Activity by clicking the first notification, set the second notification using ALARM manager.

Akhil
  • 13,888
  • 7
  • 35
  • 39
  • all the notifications(for the future events) are getting fired . Eg: Event1 10:00am Event2 10:05am. At 10:00am, both, Event1 and Event2 notifications are fired At 10:05am, only Event2 is fired. – Sherlock Apr 06 '12 at 07:25