Реализация локальных уведомлений (Local Notifications) в мобильном приложении
Локальные уведомления — единственный тип, который не требует сервера вообще. Приложение само планирует их через системный API: через конкретное время, по триггеру календаря или при входе в геозону. Типичные применения — напоминания, трекеры привычек, события в оффлайн-режиме.
iOS: UNUserNotificationCenter
Весь API с iOS 10+ — через UNUserNotificationCenter. Три типа триггеров:
import UserNotifications
// 1. По времени (через N секунд)
let content = UNMutableNotificationContent()
content.title = "Напоминание о встрече"
content.body = "Встреча с командой через 15 минут"
content.sound = .default
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 900, repeats: false)
let request = UNNotificationRequest(identifier: "meeting-reminder-123",
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request)
// 2. По дате/времени (повторяющееся каждый день в 9:00)
var dateComponents = DateComponents()
dateComponents.hour = 9
dateComponents.minute = 0
let dailyTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// 3. По геозоне
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 50.45, longitude: 30.52),
radius: 200,
identifier: "office-zone")
region.notifyOnEntry = true
region.notifyOnExit = false
let geoTrigger = UNLocationNotificationTrigger(region: region, repeats: false)
Идентификатор запроса (identifier) — важен. По нему обновляем или отменяем уведомление:
// Обновить (перезаписать существующее)
UNUserNotificationCenter.current().add(updatedRequest) // тот же identifier
// Отменить
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["meeting-reminder-123"])
// Посмотреть запланированные
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
print("Pending: \(requests.count)")
}
Лимит iOS — 64 запланированных уведомления на приложение. При превышении старые молча удаляются. Для трекеров с сотнями напоминаний нужна стратегия: держать в очереди ближайшие N, остальные перепланировать при открытии приложения.
Android: NotificationCompat + AlarmManager / WorkManager
На Android нет единого «notification scheduler» — нужно самостоятельно сохранять время, будить приложение через AlarmManager и тогда показывать уведомление.
// Планирование через AlarmManager (точное время — требует SCHEDULE_EXACT_ALARM на Android 12+)
val alarmManager = context.getSystemService(AlarmManager::class.java)
val intent = Intent(context, NotificationReceiver::class.java).apply {
putExtra("title", "Напоминание о встрече")
putExtra("body", "Встреча через 15 минут")
putExtra("notification_id", 123)
}
val pendingIntent = PendingIntent.getBroadcast(context, 123, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
triggerAtMillis,
pendingIntent
)
NotificationReceiver — BroadcastReceiver, который строит и показывает уведомление:
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val notification = NotificationCompat.Builder(context, "reminders_channel")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(intent.getStringExtra("title"))
.setContentText(intent.getStringExtra("body"))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build()
NotificationManagerCompat.from(context)
.notify(intent.getIntExtra("notification_id", 0), notification)
}
}
Важно: AlarmManager сбрасывается при перезагрузке устройства. Нужно перехватывать BOOT_COMPLETED broadcast и перепланировать все активные напоминания из локальной БД (Room).
Для Android 12+ точные алармы требуют разрешения SCHEDULE_EXACT_ALARM. Для повторяющихся напоминаний без точного времени — лучше WorkManager с PeriodicWorkRequest, он пережит reboot автоматически.
Геозонные уведомления
На iOS геозонные уведомления — через UNLocationNotificationTrigger (см. выше). На Android — через GeofencingClient:
val geofence = Geofence.Builder()
.setRequestId("office-zone")
.setCircularRegion(50.45, 30.52, 200f)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build()
val request = GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofence(geofence)
.build()
geofencingClient.addGeofences(request, geofencePendingIntent)
Геозонные уведомления требуют разрешения ACCESS_FINE_LOCATION и на Android 10+ — ACCESS_BACKGROUND_LOCATION. Последнее — отдельный запрос, пользователь должен явно выбрать «Разрешить всегда» в настройках.
Сроки
Реализация локальных уведомлений по времени и расписанию, геозонные триггеры, корректная обработка reboot и лимитов — 3–6 рабочих дней в зависимости от платформ.







