Local notifications not showing up when device is rebooted (or similar state) Android

I'm a bit new so I have a simple question, I have created a Xamarin.Android app and I have used local notifications with alarm manager, broadcast receivers, etc to schedule them, the problem that I have is that my scheduled notifications are not being showed when the device is rebooted, or completely off state and turn on again. My questions are:

Is that a problem inside Android? otherwise How can I solve it?

I hope help, thanks

Upvotes: 0

Views: 413

Answers (1)

Blundell
Blundell

Reputation: 76506

By default, all alarms are canceled when a device shuts down.

https://developer.android.com/training/scheduling/alarms

By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.

You need to monitor for BOOT_COMPLETE and re-set your alarms.

https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED

Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<receiver android:name=".SampleBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>

Receiver:

class SampleBootReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED") {
            // Re-set the alarm here.
        }
    }
}

Upvotes: 1

Related Questions