1

I'm having some trouble with getting a daily notification appearing.

My application gets a time, a hour and minute that the user wishes to be reminded of something.

I then use AlarmManager to set up a alarm at this time repeating whilst using alarm receiver to create the notification.

I've been trying this for hours but can't figure out what I'm doing wrong.

I've looked at a bunch of other SO questions but none have helped me yet.

I've stored the user's hours and minute input into a date object get habitReminder.

My createNotifications() method:

private void createNotifications() {
    Log.i("Reminder at",""+habitReminder.getHours()+":"+habitReminder.getMinutes());
    //Create the alarms/notifications for the user
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    alarmIntent.putExtra("name", habitName);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    Log.i("createNotifications", "Alarm manager is created.");

    //Set the timing of the reminder
    Calendar calendar = Calendar.getInstance();
    Calendar now = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, habitReminder.getHours());
    calendar.set(Calendar.MINUTE, habitReminder.getMinutes());
    calendar.set(Calendar.SECOND,0);

    //Check to make sure time is after the current date.
    if(calendar.before(now)){
        calendar.add(Calendar.DATE, 1);
    }

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    Log.i("createNotifications", "Alarm has been set for " +habitReminder.getHours()+":"+habitReminder.getMinutes() +" daily.");
}

My alarm receiver class:

public class AlarmReceiver extends BroadcastReceiver {

private static int id =0;

@Override
public void onReceive(Context context, Intent intent) {
    String name = intent.getStringExtra("name");
    String title = name + " Reminder!";
    String message = "Your reminder to keep up your habit!";
    long when = System.currentTimeMillis();
    Intent in = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,in,PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationManager nM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context)
            .setContentIntent(contentIntent)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true)
            .setWhen(when);
    Notification notification = builder.build();
    nM.notify(id,notification);
    id++;
}

}

And my android manifest:

<receiver android:name="com.closedbracket.trackit.AlarmReceiver" android:enabled="true">
</receiver>

Any help would really be appreciated.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
ZarifS
  • 467
  • 1
  • 11
  • 20
  • can you put a `Log` inside your `onReceive()` and check if that is getting triggered at the set time. – sagar suri Aug 08 '17 at 02:35
  • I have an example for `setExact(AlarmManagaer...)`, it works for me so it might help you, [create and set the AlarmManager](https://stackoverflow.com/a/44303234/5212904), it also explains how to cancel the `AlarmManager` from another `Activity`. Then if you get it all the way to the receiver then you are half way done and the question would be whether the `Notification` is correctly implemented or not. – ArtiomLK Aug 08 '17 at 03:56

2 Answers2

1

If you want a precise and reliable alarm, use setAlarmClock. It will drain more power from the battery but you are sure the alarm will ring precisely at the time set.

For more information, you can refer to Difference between setExact and setAlarmClock

Zelig63
  • 1,592
  • 1
  • 23
  • 40
  • I want the alarm to repeat however, every day in this case. So if the user sets it at a time, it will send the alarm which is really just a notification to remind the user of something at that time. – ZarifS Aug 08 '17 at 20:46
  • 1
    If you want a repetition, you can program a Service to execute at the alarm time and make it trigger a new alarm for the next day. – Zelig63 Aug 09 '17 at 05:39
0

I think you have the arguments to setRepeating wrong. 2nd argument is the time interval until first alarm fires (the jitter). 3rd is the interval, after the first occurrence at which subsequent alarms fire (the interval).

You are setting an alarm for AlarmManager.INTERVAL_DAY which has a value of 86400000. Your alarm will fire once a day.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • Yes thats correct, if the user sets the alarm for 8:30am, i put that time as the 2nd argument, and have it repeating daily. At least thats what i was going for, am I doing that wrong? – ZarifS Aug 08 '17 at 20:45
  • In that case, @zelig63 has the right answer. The system can move repeating alarms by as much as 20% of their interval and may not wake the system at all, if it is asleep. Use `setAlarmClock` to have your alarm delivered reliably, at an exact time. You'll have to reset it yourself... – G. Blake Meike Aug 08 '17 at 22:32