Search code examples
androidandroid-intentandroid-broadcastandroid-alarms

Android alarm to trigger notification


I need the Android AlarmManager to trigger a notification.

I'm creating the alarm as follows:

    private void createAlarm() {
    AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent mNotificationReceiverIntent = new Intent(MainActivity.this,AlarmNotificationReceiver.class);
    PendingIntent mNotificationReceiverPendingIntent =
            PendingIntent.getBroadcast(MainActivity.this,0,mNotificationReceiverIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    mAlarmManager.setRepeating(AlarmManager.RTC, SystemClock.currentThreadTimeMillis()
            + ALARM_DELAY, ALARM_DELAY, mNotificationReceiverPendingIntent);
    Log.i(TAG,"Alarm created");
    }

The AlarmNotificationReceiver is

public class AlarmNotificationReceiver extends BroadcastReceiver {

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

    Log.i(TAG,"Alarm Notification received");
}
}

I never got to see the logmessage, though. I've seen the alarm firing using "adb shell dumpsys alarm":

  u0a54:de.fabian_nick.dailyselfie +709ms running, 0 wakeups:

+709ms 0 wakes 12 alarms: *alarm*:de.fabian_nick.dailyselfie/.AlarmNotificationReceiver

(at least if I read this correctly). So I'm assuming the PendingIntent I created is for some reason not being picked up by the BroadcastReceiver. What's wrong with my code?


Solution

  • I needed to add

    <receiver android:name=".AlarmNotificationReceiver"></receiver>
    

    to my AndroidManifest.xml