-
-
Notifications
You must be signed in to change notification settings - Fork 72
4. Repeat Notification
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.
- Notifications must be scheduled (using
NotifyTime) -
NotifyTimeis aDateTimeOffset?— useDateTimeOffset.Now(notDateTime.Now) so timezone information is preserved correctly - Proper permissions must be granted
- Consider platform-specific limitations
The plugin supports four types of recurring notifications:
Notification repeats every day at the same time.
using Plugin.LocalNotification;
using Plugin.LocalNotification.Core.Models;
var dailyReminder = new NotificationRequest
{
NotificationId = 100,
Title = "Daily Reminder",
Description = "This notification appears every day",
Schedule =
{
NotifyTime = DateTimeOffset.Now.Date.AddHours(9), // 9:00 AM
RepeatType = NotificationRepeat.Daily
}
};
await LocalNotificationCenter.Current.Show(dailyReminder);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 = DateTimeOffset.Now.Date.AddDays(
((int)DayOfWeek.Monday - (int)DateTimeOffset.Now.DayOfWeek + 7) % 7
).AddHours(10), // Next Monday at 10:00 AM
RepeatType = NotificationRepeat.Weekly
}
};
await LocalNotificationCenter.Current.Show(weeklyReminder);Notification repeats after a specified time interval.
var intervalReminder = new NotificationRequest
{
NotificationId = 102,
Title = "Periodic Update",
Description = "Checking for updates",
Schedule =
{
NotifyTime = DateTimeOffset.Now.AddMinutes(30), // First notification after 30 minutes
RepeatType = NotificationRepeat.TimeInterval,
NotifyRepeatInterval = TimeSpan.FromHours(2) // Then every 2 hours
}
};
await LocalNotificationCenter.Current.Show(intervalReminder);Notification repeats on the same day of each month at the same time.
var monthlyReminder = new NotificationRequest
{
NotificationId = 103,
Title = "Monthly Report",
Description = "Time to submit your monthly report",
Schedule =
{
NotifyTime = DateTimeOffset.Now.AddMonths(1), // First fire next month, same day
RepeatType = NotificationRepeat.Monthly
}
};
await LocalNotificationCenter.Current.Show(monthlyReminder);Note: When the schedule day does not exist in the next month (e.g., 31st of a month without 31 days),
DateTimeOffset.AddMonthsclamps to the last day of that month — consistent with standard .NET calendar arithmetic.
- TimeInterval notifications cannot have both delay and repeat
- Use Daily or Weekly repeat for more precise timing
- Background refresh may affect timing
- Battery optimization may affect timing
- Doze mode can delay notifications
- Consider using Foreground Service for critical timing
await LocalNotificationCenter.Current.Cancel(notificationId);// 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);var pendingNotifications = await LocalNotificationCenter.Current.GetPendingNotificationList();
foreach (var notification in pendingNotifications)
{
if (notification.Schedule.RepeatType != NotificationRepeat.No)
{
// Handle repeating notification
}
}-
Unique IDs
- Use different ID ranges for different types of notifications
- Keep track of notification IDs for management
-
Timing Considerations
// Bad: Might miss notifications if app is closed NotifyTime = DateTimeOffset.Now.AddMinutes(1) // Good: Use future time that won't be missed NotifyTime = DateTimeOffset.Now.AddMinutes(5)
-
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}"); }
-
Resource Management
- Limit the number of concurrent repeating notifications
- Clean up unused notifications
- Consider battery and system resource impact
Check:
- Correct RepeatType is set
- NotifyTime is in the future
- Platform-specific restrictions
- Battery optimization settings
Solutions:
- Use longer intervals (15+ minutes)
- Implement retry logic
- Consider using push notifications
- Check system clock synchronization
Recommendations:
- Use appropriate intervals (not too frequent)
- Clean up unnecessary notifications
- Monitor battery usage
- Implement user controls for frequency
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;
}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 = AndroidPriority.High,
ChannelId = "health_reminders"
}
};