Android notifications multiple times

2019-05-26 08:19发布

问题:

I have notifications that remind user to take the medicine. When I add new times for taking medicines, for example, at 9:00 pm and 9:01 pm, only last notification is active. Here is my code:

AddTime.java: (part)

Cursor cursor2 = sqdb.rawQuery("SELECT Name FROM Medicine WHERE _id = " + mId, null);
        while(cursor2.moveToNext()) {
            med = cursor2.getString(cursor2.getColumnIndex("Name"));
        }  //med - Name of medicine

        String sql = "SELECT _id FROM Time WHERE mId = "+mId+" AND Year = "+year+" AND Month = '"+_month+"' AND Day = "+_numDay+" AND Week = '"+_week+"' AND Hour = "+_hour+" AND Minute = "+_min+" AND AMORPM = '"+_ampm+"' AND CalendarDate = '"+calDate+"'";
        Cursor cursor = sqdb.rawQuery(sql, null);
        int tId = 0;
        while(cursor.moveToNext()) {
            tId = cursor.getInt(cursor.getColumnIndex("_id"));
        } //Unique id for notification

        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent _intent = new Intent(this, TimeAlarm.class);
        _intent.putExtra("TIMEID", tId);
        _intent.putExtra("MEDNAME", med);
        Calendar c = Calendar.getInstance();
        int Years = c.get(Calendar.YEAR);
        int Months = c.get(Calendar.MONTH);
        int Days = c.get(Calendar.DAY_OF_MONTH);
        int Hours = c.get(Calendar.HOUR_OF_DAY);
        int Minutes = c.get(Calendar.MINUTE);

        GregorianCalendar curDate = new GregorianCalendar(Years, Months, Days, Hours, Minutes, 0);
        GregorianCalendar nextDate = new GregorianCalendar(year, Month, _numDay, _hour, _min, 0);

        long millisecDiff = nextDate.getTimeInMillis() - curDate.getTimeInMillis();

        PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _intent, PendingIntent.FLAG_ONE_SHOT);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);

TimeAlarm.java

public class TimeAlarm extends BroadcastReceiver {

 NotificationManager nm;


 @SuppressWarnings("deprecation")
@Override
 public void onReceive(Context context, Intent intent) {
  nm = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  int timeid = intent.getExtras().getInt("TIMEID");
  String medname = intent.getExtras().getString("MEDNAME");
      CharSequence from = "Title";
  CharSequence message = "Message";
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    new Intent(), 0);
  Notification notif = new Notification(R.drawable.icon,
    "Text", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  nm.notify(timeid, notif);
 }
}

回答1:

The second parameter in your PendingIntent.getBroadcast known as the request code needs to be unique for each new alarm, otherwise it will overwrite. It says in the documentation that this requestCode is not currently used. The documentation is wrong.

you have

PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _intent, PendingIntent.FLAG_ONE_SHOT); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);

What you want is

PendingIntent pIntent = PendingIntent.getBroadcast(this, SomeUniqueIndex, _intent, PendingIntent.FLAG_ONE_SHOT); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisecDiff, pIntent);