Repository navigation
fix: scheduled local notifications on Android and iOS - #181
Open
mirzemehdi wants to merge 2 commits into
Open
mirzemehdi wants to merge 2 commits into
mirzemehdi wants to merge 2 commits into
Conversation
NotificationReceiver only emitted EVENT_SCHEDULED_NOTIFICATION_FIRED_INTERNAL and never posted the notification, so scheduled notifications never showed on Android (broken since scheduling was added in #162). - Store title, body, payload, image, actions and the Android configuration in the alarm intent; the receiver rebuilds and posts the notification, falling back to the stored configuration in a cold process before KMPNotifier.initialize. The onAction event is still emitted afterwards. - Extract notification building into AndroidNotificationPoster, shared by AndroidNotifier and NotificationReceiver. - remove(id): build the cancel intent with the receiver component so it matches the scheduled PendingIntent and the alarm is actually cancelled. - Resolve relative scheduledAt before setting the alarm (it fired immediately before). - Check canScheduleExactAlarms() on API 31+ and use setAndAllowWhileIdle when exact alarms aren't permitted instead of catching SecurityException. - Sample app: drop the workaround that re-posted on the scheduled event.
- Emit the scheduled-fired onAction event (EVENT_SCHEDULED_NOTIFICATION_FIRED_INTERNAL) from willPresent for scheduled local notifications, matching Android. Only observable in the foreground; iOS presents background notifications itself. - Schedule with UNCalendarNotificationTrigger for the absolute fire date (time zone pinned, rounded up to the next second); immediate notifications use a nil trigger instead of a 1s interval. - Await action-category registration before adding the request, and replace an existing category for the same id instead of accumulating duplicates. - Attach a temp copy of NotificationImage.File images; the system moves attachment files, which deleted the caller's file. - Detect the image extension from the data (PNG/JPEG/GIF) instead of always writing .jpg.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Android:
notify { scheduledAt = <future> }sets an alarm, but when it firesNotificationReceiveronly logsScheduled notification triggered: <id>and emitsEVENT_SCHEDULED_NOTIFICATION_FIRED_INTERNALthroughonAction. It never callsNotificationManager.notify, and title, body, image and actions are not stored anywhere. So scheduled notifications never show. This has been the case since scheduling was added in #162; the sample app hid it by re-posting fromonAction.iOS: scheduled notifications are delivered, but behavior differs from Android and the attachments have problems:
IOS_ACTION_SCHEDULED_FIREDis defined but unused.NotificationImage.Fileattaches the caller's file directly. The system moves attachment files, so the original was deleted..jpg. iOS infers the type from the extension, so PNG/GIF could be rejected.Changes — Android
goAsync(), then emits theonActionevent as before. It uses the live configuration when available and falls back to the stored one in a cold process, beforeKMPNotifier.initialize. Internal extras are removed from the emitted payload. Alarms set by older versions (without stored content) fall back to the old emit-only behavior.AndroidNotificationPosternow holds the notification-building code, so immediate and scheduled notifications produce identical results.remove(id)cancels alarms. The cancel intent had no component, soFLAG_NO_CREATEnever matched the scheduledPendingIntent.scheduledAt. The raw value was passed toAlarmManager, so the alarm fired immediately. The fire time is now resolved once and reused.canScheduleExactAlarms()is checked first, andsetAndAllowWhileIdleis used otherwise. No moreSecurityExceptionon every scheduled call.Changes — iOS
willPresentemitsEVENT_SCHEDULED_NOTIFICATION_FIRED_INTERNALfor scheduled local notifications. This only happens in the foreground: iOS presents notifications itself while the app is in the background, so there is no hook there.UNCalendarNotificationTriggerfor the absolute fire date, with the time zone pinned and the time rounded up to the next second. If less than 1s remains, a 1s interval trigger is used. Immediate notifications now use aniltrigger, so they no longer wait 1 second..jpg.Other
Unreleased→ Fixed entries for both platforms. Behavior note: apps that used the re-post workaround must remove it on both platforms.apiCheckpasses); all new code isinternal.Tests
Android (Robolectric,
androidHostTest):onAction, and strips internal keys from the payloadremove(id)cancels the pending alarm (checked to fail on the old code)scheduledAtschedules in the futurecanScheduleExactAlarms()iOS (
iosTest, simulator):nextTriggerDateequals the fire date.jpgfallbacks./gradlew :kmpnotifier-local:testAndroidHostTest :kmpnotifier-local:iosSimulatorArm64Test apiCheckpasses. Not yet checked end-to-end on a device or emulator (Android alarm in a killed process, iOS foreground event and attachments).