Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Daily notification using alarm manager

I have written this code that will be executed everyday and display a notification:

class DailyNotification extends BroadcastReceiver {

    // Register the alarm and set it at 7am everyday (repeating mode)
    public static void registerAlarm(Context paramContext) {
        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
            calendar.add(7, 1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);

        // PendingIntent that will perform a broadcast
        PendingIntent localPendingIntent = PendingIntent
                .getBroadcast(
                        paramContext,
                        22341,
                        new Intent(
                                "com.bestweightmanager.example.exampledailynotification.DAILY_NOTIFICATION"),
                        PendingIntent.FLAG_UPDATE_CURRENT);
        // Retrieve an AlarmManager to set a repeating daily alarm
        ((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
                calendar.getTimeInMillis(), 1000,
                localPendingIntent);
    }
}

The manifiest file looks like the following:

<receiver
    android:name=".utils.DailyNotification"
    android:process=":remote" >
    <intent-filter>
        <action android:name="com.bestweightmanager.example.exampledailynotification.DAILY_NOTIFICATION" />
        <action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

But I don't get any notifications. Can anyone suggest me how to solve this problem?

Also, that what is meaning of below code

Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
    calendar.add(7, 1);
}
like image 668
Siddhpura Amit Avatar asked Feb 18 '26 05:02

Siddhpura Amit


1 Answers

Answering in parts.

1.

Replace the line in your code

((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
                calendar.getTimeInMillis(), 1000,
                localPendingIntent);

with this line:

((AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE)).setRepeating(1,
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                localPendingIntent);

You actually need to get system service - Context.ALARM_SERVICE

And instead of setting the repeat frequency to 1000 ms, you need to use AlarmManager.INTERVAL_DAY, so as to trigger it daily on the desired set time.

2.

About the meaning of this code:

Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
    calendar.add(7, 1);
}

Here calendar.add(7, 1) does not makes perfect sense to me. The add() function of calendar is used to add a given amount to a specific Calendar field, identified by a unique integer.

If your code line calendar.add(7, 1); is replaced with calendar.add(Calendar.DATE, 1);, than this code might make some sense. It will than actually check that while registering the alarm, if the current Hour of Day is greater than 7 (i.e, current day time has exceeded 7 AM), than it will set the calendar object (which is later used to register alarm) for 7 AM of next date (tomorrow).

3.

Also I am doubtful about the declaration in your manifest file. You need to crosscheck it with the source from where you are referring.

In general, the structure of manifest file for AlarmManager should look somewhat like this:

<application>

    <activity>
        <intent-filter>
            <action/>

            <category/>
        </intent-filter>
    </activity>

    <receiver android:name=".DailyNotification" />
</application>

Hope that helps.

like image 99
AnniJais Avatar answered Feb 19 '26 19:02

AnniJais



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!