0

I want to build a water reminder app. This app should send notifications every time you are supposed to drink water, according to some calculations that the app does beforehand. I know how to use an AlarmManager to schedule one-time alarms and repeated alarms. What I haven't figured out, after searching quite a lot, is how I am going to use an AlarmManager to schedule a specific number of alarms (say 6) every day, stop at a specific time during the day (say 10.00pm) and start send notifications the next day at a specific time (say 07.00am). All of this should happen without the user having to open the app. How do I do this? Here's my code:

Part of MainActivity:

Long alertTime = nextAlarm.getTimeInMillis();
    Intent alertIntent = new Intent(this, AlertReceiver.class);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
            PendingIntent.getBroadcast(this, 1, alertIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT));

AlertReceiver:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class AlertReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent



intent) {
        createNotification(context, "Hydrate", "Time to drink water", "Alert");
    }

public void createNotification(Context context, String msg, String msgText, String msgAlert) {

    PendingIntent notificationIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_drop_24dp)
            .setContentTitle(msg)
            .setContentText(msgText)
            .setTicker(msgAlert);

    notificationBuilder.setContentIntent(notificationIntent);

    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);

    notificationBuilder.setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1, notificationBuilder.build());

    }

}

Thanks for the help!

A. Savva
  • 632
  • 10
  • 30
  • Possible duplicate of [android send notification id to receiver while set alarm](http://stackoverflow.com/questions/35918099/android-send-notification-id-to-receiver-while-set-alarm) – Dario Apr 19 '16 at 13:12
  • Either set six daily repeating alarms for the desired times, or, set a single repeating alarm each day (provided that the interval is constant), and cancel it the sixth time it fires, rescheduling for the next day. – Mike M. Apr 19 '16 at 13:17

1 Answers1

1

If you want to send notifications in fixed intervals, set repeated alarms with fixed intervals and in the Broadcast Receiver, check the current time and send notification.

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

    //get current hour in 24 hour format
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int curHour = calendar.get(Calendar.HOUR_OF_DAY);

    if(curHour > 7 && curHour < 22)
        createNotification(context, "Hydrate", "Time to drink water", "Alert");
}
AnUp
  • 21
  • 1
  • 2