ZarifS
ZarifS

Reputation: 477

Alarm Notifications not appearing

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.

Upvotes: 1

Views: 96

Answers (2)

G. Blake Meike
G. Blake Meike

Reputation: 6715

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.

Upvotes: 0

Zelig
Zelig

Reputation: 1808

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

Upvotes: 1

Related Questions