Skip to content

4. Repeat Notification

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

Repeating Notifications Guide

Overview

Repeating notifications are useful for recurring reminders, periodic updates, or any scenario where you need to notify users regularly. This guide covers how to implement different types of recurring notifications.

Prerequisites

  • Notifications must be scheduled (using NotifyTime)
  • Proper permissions must be granted
  • Consider platform-specific limitations

Repeat Types

The plugin supports three types of recurring notifications:

1. Daily Repeat

Notification repeats every day at the same time.

var dailyReminder = new NotificationRequest
{
    NotificationId = 100,
    Title = "Daily Reminder",
    Description = "This notification appears every day",
    Schedule = 
    {
        NotifyTime = DateTime.Today.AddHours(9), // 9:00 AM
        RepeatType = NotificationRepeat.Daily
    }
};
await LocalNotificationCenter.Current.Show(dailyReminder);

2. Weekly Repeat

Notification repeats on the same day each week at the same time.

var weeklyReminder = new NotificationRequest
{
    NotificationId = 101,
    Title = "Weekly Meeting",
    Description = "Team sync every Monday",
    Schedule = 
    {
        NotifyTime = DateTime.Today.AddDays(
            ((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek + 7) % 7
        ).AddHours(10), // Next Monday at 10:00 AM
        RepeatType = NotificationRepeat.Weekly
    }
};
await LocalNotificationCenter.Current.Show(weeklyReminder);

3. Time Interval

Notification repeats after a specified time interval.

var intervalReminder = new NotificationRequest
{
    NotificationId = 102,
    Title = "Periodic Update",
    Description = "Checking for updates",
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddMinutes(30), // First notification after 30 minutes
        RepeatType = NotificationRepeat.TimeInterval,
        NotifyRepeatInterval = TimeSpan.FromHours(2) // Then every 2 hours
    }
};
await LocalNotificationCenter.Current.Show(intervalReminder);

Platform-Specific Considerations

iOS Limitations

  • TimeInterval notifications cannot have both delay and repeat
  • Use Daily or Weekly repeat for more precise timing
  • Background refresh may affect timing

Android Considerations

  • Battery optimization may affect timing
  • Doze mode can delay notifications
  • Consider using Foreground Service for critical timing

Managing Repeating Notifications

Cancel a Repeating Notification

await LocalNotificationCenter.Current.Cancel(notificationId);

Update a Repeating Notification

// Cancel existing notification
await LocalNotificationCenter.Current.Cancel(notificationId);

// Create new notification with updated parameters
var updatedNotification = new NotificationRequest
{
    NotificationId = notificationId,
    Title = "Updated Reminder",
    Description = "Modified repeating notification",
    Schedule = 
    {
        NotifyTime = newDateTime,
        RepeatType = NotificationRepeat.Daily
    }
};
await LocalNotificationCenter.Current.Show(updatedNotification);

Check Pending Notifications

var pendingNotifications = await LocalNotificationCenter.Current.GetPendingNotifications();
foreach (var notification in pendingNotifications)
{
    if (notification.Schedule.RepeatType != NotificationRepeat.No)
    {
        // Handle repeating notification
    }
}

Best Practices

  1. Unique IDs

    • Use different ID ranges for different types of notifications
    • Keep track of notification IDs for management
  2. Timing Considerations

    // Bad: Might miss notifications if app is closed
    NotifyTime = DateTime.Now.AddMinutes(1)
    
    // Good: Use future time that won't be missed
    NotifyTime = DateTime.Now.AddMinutes(5)
  3. Error Handling

    try
    {
        await LocalNotificationCenter.Current.Show(notification);
    }
    catch (Exception ex)
    {
        // Log error and implement retry logic if needed
        Debug.WriteLine($"Failed to schedule notification: {ex.Message}");
    }
  4. Resource Management

    • Limit the number of concurrent repeating notifications
    • Clean up unused notifications
    • Consider battery and system resource impact

Common Issues and Solutions

1. Notifications Not Repeating

Check:

  • Correct RepeatType is set
  • NotifyTime is in the future
  • Platform-specific restrictions
  • Battery optimization settings

2. Irregular Timing

Solutions:

  • Use longer intervals (15+ minutes)
  • Implement retry logic
  • Consider using push notifications
  • Check system clock synchronization

3. Performance Impact

Recommendations:

  • Use appropriate intervals (not too frequent)
  • Clean up unnecessary notifications
  • Monitor battery usage
  • Implement user controls for frequency

Examples

Work Week Reminder

var workdayReminder = new NotificationRequest
{
    NotificationId = 103,
    Title = "Daily Standup",
    Description = "Time for team standup",
    Schedule = 
    {
        NotifyTime = GetNextWorkday().AddHours(9).AddMinutes(30), // 9:30 AM
        RepeatType = NotificationRepeat.Daily
    }
};

private DateTime GetNextWorkday()
{
    var date = DateTime.Today;
    while (date.DayOfWeek == DayOfWeek.Saturday || 
           date.DayOfWeek == DayOfWeek.Sunday)
    {
        date = date.AddDays(1);
    }
    return date;
}

Health App Style Reminder

var healthReminder = new NotificationRequest
{
    NotificationId = 104,
    Title = "Activity Check",
    Description = "Time to move!",
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddHours(1),
        RepeatType = NotificationRepeat.TimeInterval,
        NotifyRepeatInterval = TimeSpan.FromHours(2)
    },
    Android = 
    {
        Priority = NotificationPriority.High,
        ChannelId = "health_reminders"
    }
};

Clone this wiki locally