Rueben
Rueben

Reputation: 135

How to set up Multiple Notifications through Alarm Manager?

I have String time values stored in a db. I can access these with the below code, which will bring back and log all the time values.

I need to send notifications to the user for these separate time values but I am not succeeding. It only seems to send me a notification for the last entry I entered, why is this?

Broadcast Receiver has onRecieve Method

@Override
public void onReceive(Context context, Intent intent)
{

    helper = new TaskDBHelper(context);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
            null, null, null, null, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        Name = cursor.getString(1);
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_cast_light)
                        .setContentTitle("Please Take " + Name)
                        .setContentInfo("Time")
                        .setContentText("Enjoy" )
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setAutoCancel(true)
                        .setVibrate(new long[]{500, 500, 500, 500}) //set virbrate & color of led
                        .setLights(Color.BLUE, 3000, 3000);

        NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(getID(), builder.build());
        cursor.moveToNext();
    }

}

Method to find and loop through the values in the db for time.

   private void setUpNot(){
    helper = new TaskDBHelper(Meds2.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();

    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
            null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) { // Loop until all vales have been seen

        String time = cursor.getString(2);
        String[] parts = time.split(":"); //Split  String Value stored in db
        String part1 = parts[0]; // hour
        String part2 = parts[1]; // minute
        int hr = Integer.parseInt(part1);
        int min = Integer.parseInt(part2);

        final int _id=(int)System.currentTimeMillis();

        Intent alarmIntent = new Intent(Meds2.this, AlarmReceiver.class); //set up alarm
        pendingIntent = PendingIntent.getBroadcast(Meds2.this, _id, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hr);
        calendar.set(Calendar.MINUTE, min); //set cal time based of db value

                    /* Repeating on every 24 hours interval */
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent); // run every date.time() in millisec

        Log.e(TAG, "DATE VALUE TIME IS " + part1 +"--" +part2); //log part one and two to make sure time is right

        cursor.moveToNext();
    }
}

getid()

 public static int getID() {
    AtomicInteger c = new AtomicInteger(0);
    return c.incrementAndGet();
}

Upvotes: 0

Views: 128

Answers (1)

Shadab Ansari
Shadab Ansari

Reputation: 7070

The problem is with your getID() method.

Your getID() method will always return 1 and hence making the notification id '1' everytime, which means that you are always updating the notification instead of creating a new one. And that's why you are getting only one notification even though you are creating multiple ones.

You have to modify your getID() method like this:

static AtomicInteger c = new AtomicInteger(0);

public static int getID() {
   return  c.incrementAndGet();
}

By doing this, you will get a different id everytime and hence different notifications.

Upvotes: 1

Related Questions