0

I have an app that sends notifications to the status bar as a reminder to take a pill. If two pills need to be taken at the same time. then two separate notifications shall be sent. I know you need to use an unique id for this to happen in the .notify(int id, notification notification) function. However it doesn't seem to be working. I know for sure my ids are different as I've tested them by displaying a toast. I would appreciate any suggestions. Heres my code:

Alarm class:

DatabaseHelper notiDb = new DatabaseHelper(this);
    notiDb.open();
    final String dataName = notiDb.getDataName(pushed_name);
    String dataDaily = notiDb.getDataDaily(pushed_name);
    String dataWeekly = notiDb.getDataWeekly(pushed_name);
    String dataTwice = notiDb.getDataTwice(pushed_name);
    String dataDosage = notiDb.getDataDosage(pushed_name);
    String dataStart = notiDb.getDataStart(pushed_name);
    String dataID = notiDb.getDataID(pushed_name);
    notiDb.close();

    ID = Integer.parseInt(dataID);
    Calendar uncalendar = Calendar.getInstance();
    String unID = dataID +uncalendar;


    Toast toast=Toast.makeText(this, "Alarm class, Notification for " +dataName +" has been set id: " +ID, Toast.LENGTH_LONG);  
    toast.show();

   Intent intent_unique = new Intent(this, NotiScenario.class);
   intent_unique.putExtra("ID", ID);
   intent_unique.setData(Uri.parse(intent_unique.toUri(Intent.URI_INTENT_SCHEME)));
   PendingIntent pIntent = PendingIntent.getActivity(this, ID, intent_unique, 2);


    // Build notification
    Notification noti = new Notification.Builder(this)
        .setContentTitle("MedScan")
        .setContentText("3. You should take "+dataDosage +" pills of " +dataName)
        .setSmallIcon(R.drawable.original)
        .setContentIntent(pIntent)
       .build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    noti.defaults |= Notification.DEFAULT_ALL;
     //Hide the notification after its selected
   noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(ID, noti);

Info class:

DatabaseHelper notiDb = new DatabaseHelper(this);
    notiDb.open();
    final String dataName = notiDb.getDataName(pushed_name);
    String dataDaily = notiDb.getDataDaily(pushed_name);
    final String dataWeekly = notiDb.getDataWeekly(pushed_name);
    String dataTwice = notiDb.getDataTwice(pushed_name);
    final String dataDosage = notiDb.getDataDosage(pushed_name);
    String dataStart = notiDb.getDataStart(pushed_name);
    String dataID = notiDb.getDataID(pushed_name);
    notiDb.close();

     ID = Integer.parseInt(dataID);
     int value_Weekly = Integer.parseInt(dataWeekly);
     int value_Daily = Integer.parseInt(dataDaily);
     int value_Twice = Integer.parseInt(dataTwice);
     int value_Start = Integer.parseInt(dataStart);

     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, value_Start);
     calendar.set(Calendar.MINUTE, 46);
     calendar.set(Calendar.SECOND, 00);
     int setTime = (value_Daily*value_Twice)/value_Weekly;



     Intent intent_noti = new Intent(this,Alarm.class);
     intent_noti.putExtra("ID", ID);
     intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));
     PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_ONE_SHOT);
     AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), setTime, pendingIntent);
anaconda122
  • 17
  • 1
  • 6

1 Answers1

4

The problem isn't in your Alarm class which creates the notification but in your Info class which creates an alarm to call the Info class on a regular basis. The two lines creating the pending intent for your alarm are:

Intent intent_noti = new Intent(this,Alarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_CANCEL_CURRENT);

Because the Intent is the same regardless of your ID, a newly created PendingIntent will just cancel the previously created one. In order for your code to work the Intent has to be different according to the Intent.filterEquals() method: http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)

To get a unique Intent you could do:

intent_noti.setAction(""+System.currentTimeMillis());

or:

intent_noti.putExtra("ID", ID);
intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • Thanks. Its making two notifications because its vibrating twice now, but it still only displays one notification in status bar. Ive used the intent.putExtra and intent.setData stuff in the alarm class as well. Any suggestions to get two separate notifications in the status bar? – anaconda122 Mar 17 '13 at 16:26
  • So the problem is that if two notifications are required at the same time, only the most recent one that was started in the info class gets displayed, but it still vibrates twice. If the notifications are not required at the same time, then two separate notifications are displayed. I need two separate notifications even if they are at the same time. Heres my updated code and thanks again: – anaconda122 Mar 18 '13 at 19:51
  • ID = Integer.parseInt(dataID); Calendar uncalendar = Calendar.getInstance(); Intent intent_unique = new Intent(this, NotiScenario.class); intent_unique.putExtra("ID", ID); intent_unique.setData(Uri.parse(intent_unique.toUri(Intent.URI_INTENT_SCHEME))); PendingIntent pIntent = PendingIntent.getActivity(this, ID, intent_unique, 2); – anaconda122 Mar 18 '13 at 19:52
  • Notification noti = new Notification.Builder(this) .setContentTitle("MedScan") .setContentText("3. You should take "+dataDosage +" pills of " +dataName) .setSmallIcon(R.drawable.original) .setContentIntent(pIntent) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); noti.defaults |= Notification.DEFAULT_ALL; //Hide the notification after its selected noti.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(ID, noti); – anaconda122 Mar 18 '13 at 19:53
  • Sorry, just edited it now. Still the same problem with only one notifications displaying if times are same. – anaconda122 Mar 18 '13 at 20:07