Skip to content

3. Scheduled Android notifications

Elvin Thudugala edited this page Jul 28, 2025 · 3 revisions

Scheduled Notifications in Android

Overview

This guide covers implementing scheduled notifications in Android, including potential challenges and best practices.

Implementation

Basic Scheduled Notification

var scheduledNotification = new NotificationRequest
{
    NotificationId = 100,
    Title = "Scheduled Reminder",
    Description = "This is a scheduled notification",
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddHours(1), // Schedule for 1 hour later
        RepeatType = NotificationRepeat.No // One-time notification
    }
};
await LocalNotificationCenter.Current.Show(scheduledNotification);

Recurring Notification

var recurringNotification = new NotificationRequest
{
    NotificationId = 101,
    Title = "Daily Reminder",
    Description = "This notification repeats daily",
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddDays(1),
        RepeatType = NotificationRepeat.Daily
    }
};
await LocalNotificationCenter.Current.Show(recurringNotification);

Known Limitations

1. Manufacturer-Specific Restrictions

Many Android manufacturers implement aggressive battery optimization that can affect scheduled notifications:

Manufacturer Known Issues Solution Link
Xiaomi MIUI's battery saver blocks background services Fix for Xiaomi
Huawei EMUI's protected apps list Fix for Huawei
Samsung Maximum 500 scheduled alarms Android Documentation
OnePlus Aggressive background app management Fix for OnePlus

2. System Limitations

  • Samsung devices: Maximum 500 concurrent alarms via AlarmManager
  • Android Doze mode may delay notifications
  • Background restrictions in newer Android versions

Best Practices

1. Handle Battery Optimization

public async Task CheckBatteryOptimization()
{
    if (DeviceInfo.Platform == DevicePlatform.Android)
    {
        // Request user to disable battery optimization
        await Launcher.OpenAsync("package:com.android.settings");
    }
}

2. Manage Scheduled Notifications

// Keep track of scheduled notifications
public async Task ManageScheduledNotifications()
{
    // Get all pending notifications
    var pending = await LocalNotificationCenter.Current.GetPendingNotifications();
    
    // Clean up old notifications
    foreach (var notification in pending)
    {
        if (notification.Schedule.NotifyTime < DateTime.Now)
        {
            await LocalNotificationCenter.Current.Cancel(notification.NotificationId);
        }
    }
}

3. Implement Retry Logic

public async Task ScheduleWithRetry(NotificationRequest request, int maxRetries = 3)
{
    for (int i = 0; i < maxRetries; i++)
    {
        try
        {
            await LocalNotificationCenter.Current.Show(request);
            break;
        }
        catch (Exception ex)
        {
            if (i == maxRetries - 1)
                throw;
            
            await Task.Delay(1000); // Wait before retry
        }
    }
}

User Guidance

1. Battery Optimization Settings

Instruct users to:

  1. Open device Settings
  2. Navigate to Battery/Power settings
  3. Find your app in the list
  4. Disable battery optimization or add to protected apps

2. Background Permissions

Ensure users grant necessary permissions:

  • Background execution
  • Autostart (on some devices)
  • Battery optimization exceptions

Troubleshooting Guide

1. Notifications Not Showing

Check:

  • Battery optimization settings
  • Background permissions
  • System time accuracy
  • Notification channel configuration

2. Delayed Notifications

Possible causes:

  • Doze mode
  • Battery optimization
  • System load
  • Network connectivity (if relevant)

3. Inconsistent Behavior

Solutions:

  • Test on multiple device brands
  • Implement device-specific workarounds
  • Use foreground services for critical notifications
  • Consider push notifications as alternative

Additional Resources

Reporting Issues

If you encounter device-specific problems:

  1. Document the device model and Android version
  2. Note any battery optimization settings
  3. Record the timing of the issue
  4. Share your notification scheduling code
  5. Create an issue in the GitHub repository

Clone this wiki locally