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);
有帮助吗?

解决方案

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)));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top