0

First of all I know that this question has been asked a lot , and the only answer is to set an unique id for the manager and the pending Intent so I did:

here's a code:

@Override
    public int onStartCommand(Intent i , int flags , int startId){


        try{

            //UnqueID = i.getExtras().getInt(PassedUNIQUEID);

            UniqueID++;

            Log.d("SERVICECHECK" , UniqueID + "");

            mStopIntent = new Intent(this , Service_reminder.class);

            sTimeUntilNotification = 0;

            CalculateTimeInMilSeconds(i);

            final String Title = i.getExtras().getString(PassedSharedPref);
            final String Note = i.getExtras().getString(PassedName);


            //


             Intent notificationIntent = new Intent(Service_reminder.this, activity_todialog_notecontent.class);
             notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_SUBJECTNAME, Title);
             notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_CONTENT, Note);

            PendingIntent contentIntent = PendingIntent.getActivity(Service_reminder.this, UniqueID , notificationIntent,   
                       PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

             builder =  new NotificationCompat.Builder(this).setSmallIcon(R.drawable.iconsubjecttracker)
                        .setContentTitle(Title)
                        .setContentText(Note); 

            builder.setContentIntent(contentIntent);  
            builder.setAutoCancel(true);

            Uri AlarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSound(AlarmSound);
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  

            Log.d("SERVICECHECK" , sTimeUntilNotification + "");

            CountDownTimer Timer = new CountDownTimer(sTimeUntilNotification , 1000) {

                @Override
                public void onTick(long millisUntilFinished) {

                    //manager.notify(0, builder.build());

                }

                @Override
                public void onFinish() {

                    manager.notify(UniqueID, builder.build());
                    stopService(mStopIntent);

                }
            }.start();

        }catch(NullPointerException e){

            //handle

        }



        return START_STICKY;
    }

It fires up no problem on the time , but the problem is when for example I set a reminder " to fire notification later " on a note and for example I put 2 minutes and then I go to the next note and put a reminder with and lets say a minute then the first fires up no problem and the second fires up and then updates the first ? so the user will see the last notification that was fired , I'm not sure how to fix this.

Edit:

well I'm lost , I've been trying to solve it for hours and nothing worked , from declaring a static variable and adding 1 everytime I call the service to putting an unique id in shared prefrences and load it and then update it, but nothing worked.

I edited the code to the latest thing I did which is again putting a static variable and adding one everytime since it's the cleanest solution in my opinion.

Hopefully I can find a solution tomorrow.

for anyone not wanting to read everything here's an example + a picture for what I did:

first the picture:

http://oi62.tinypic.com/2mrs9s0.jpg

as you can see I called start service twice one with 1 minute reminder and the other with 2 minutes reminder , the 1 and 2 are the Unique id , so it couldn't have used the same id.

an example of the problem:

I have a note that says " math " for example and I put a reminder after 2 minutes , and then another note that says " calculus " and I put a reminder after 1 minute , after 1 minute the notification of calculus pops with no problem , after 2 minutes only the sound pops but not the math notification , and it stays the same as if I only put a calculus note.

Edit 2:

I've solved it after using alarm manager instead of a counter down.

  • please see this http://stackoverflow.com/questions/18102052/how-to-display-multiple-notifications-in-android – Jamil Aug 05 '14 at 14:07
  • @SoftCoder well I've tried to put unique value but it doesn't work it still the same problem as I mentioned , here's a picture in the log cat that I took of the unique value that I put http://oi62.tinypic.com/2mrs9s0.jpg , as you can see the first 1 is the unique value the first time I called it the 12000 is two minutes until the notification pops up , and the 2 is the unique value when I called the service for the second time and the 60000 is one minute until the notification pops up , still it only shows the last notification but it pops the sound of the 2 minutes notification maybe updating – user3880681 Aug 05 '14 at 15:44
  • What's weird is that it only happens when there's an interval of time between the first notification and the second , so if I tried to put 0 and get a notification fast and then put 0 on the other note to get a notification , the first one doesn't get updated or deleted , it just stays there no problem. – user3880681 Aug 05 '14 at 16:01
  • in your second comment, your are saying same id remains the notification ? – Jamil Aug 05 '14 at 16:09
  • @SoftCoder , what ? the last solution that I've tried is to put a static int and increment every time onStartCommand is called as you can see in the picture , but the problem is as I said , I have a note that says " math " for example and I put a reminder after 2 minutes , and then another note that says " calculus " and I put a reminder after 1 minute , after 1 minute the notification of calculus pops with no problem , after 2 minutes only the sound pops but not the math notification , and it stays the same as if I only put a calculus note. – user3880681 Aug 05 '14 at 16:13
  • ok i understand.. let me show my code – Jamil Aug 05 '14 at 16:17

1 Answers1

0

This Method I have created in Service Class

Here SIMPLE_NOTFICATION_ID = 1

also i have tried with multiple different SIMPLE_NOTFICATION_ID and it does not remove the previous notification . means it works good.

   public void getLocation1(){

                timer = new Timer();

                locationManager.

requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);

            handler = new Handler();
            locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            mUpdateResults = new Runnable() {
                public void run() {
                    getLatLongFromDatabase();
                    if(isGPSEnabled)
                    locationlist();
                    for(int ii=0;ii<Table_Values_length ; ii++)
                    {

                    if((((Dlat[ii]-0.0002) <= lat) && (lat <= (Dlat[ii]+0.0002))) && (((Dlng[ii]-0.0002) <= lng) && (lng <= (Dlng[ii]+0.0002))) )
                    {
                        String Tooast = name[ii];

                        int idtosent = ids[ii];
                            String a = String.valueOf(lat);
                            String b = String.valueOf(lng);
                            String c = String.valueOf(idtosent);

                        // Toast.makeText(getApplicationContext(),Tooast+"-ID="+idtosent, Toast.LENGTH_SHORT).show();
                         StopContinuousRun = true;
                         Intent notifyIntent = new Intent(ServiceHandler.this, OnNotificationClick.class);
                         notifyIntent.putExtra("data1", a);
                         notifyIntent.putExtra("data2", b);  
                         notifyIntent.putExtra("data3", c);  

                         notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                         final PendingIntent Intent = PendingIntent.getActivity(getApplicationContext(), SIMPLE_NOTFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                         Notification notifyDetails  = new Notification.Builder(ServiceHandler.this)
                         .setContentTitle("You Are at "+Tooast)
                         .setContentText("Click to View This Location Tasks")
                         .setSmallIcon(R.drawable.gps_icon)
                         .setContentIntent(Intent)
                         .setAutoCancel(true)
                         .setOnlyAlertOnce(true)
                         .build();

                         notifyDetails.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
                         //  final Notification notifyDetails = new Notification(R.drawable.ic_launcher,
                       //         "You've got a new notification!",System.currentTimeMillis());
                         mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

                         mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
                        ;
                      }
                    else
                    {
                        //  Toast.makeText(getApplicationContext(),"Lat: "+lat +"\n Long: "+lng, Toast.LENGTH_SHORT).show();
                          StopContinuousRun = false;
                    }
                  }
                }
            };
            int delay = 1000 * 5; 
            int period = 8000; 

            timer.scheduleAtFixedRate(new TimerTask() {
            public void run(){handler.post(mUpdateResults); }}, delay, period);

    }
Jamil
  • 5,457
  • 4
  • 26
  • 29
  • I'm sorry if I bothered you with me , but I looked at the notification code and it's the same as mine except I'm using NotificationCompat.Builder which I need since I'm targeting api level 8 and up , yours only works on api level 11 and up. – user3880681 Aug 05 '14 at 16:39
  • Its Ok.. i will try to see again your code as soon as i get time. – Jamil Aug 05 '14 at 19:15