Compare commits
43 Commits
ios-2
...
rollover-i
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2714480070 | ||
|
|
e873a46bbd | ||
|
|
aa0eaa5389 | ||
|
|
c36781e440 | ||
|
|
cff7b659dc | ||
|
|
d3df4d9115 | ||
|
|
bc3bf484cc | ||
|
|
25f83cf1fa | ||
|
|
7188d32ae6 | ||
|
|
1157a0f1ef | ||
|
|
c2b1a60804 | ||
|
|
fa8028a698 | ||
|
|
9feaf60c84 | ||
|
|
aaeb71d31d | ||
|
|
531ce9f709 | ||
|
|
0b61d33f21 | ||
|
|
02a44a3e7b | ||
|
|
cb3cb5a78e | ||
|
|
a62f54b8a8 | ||
|
|
7702bd3b81 | ||
|
|
602eafc892 | ||
|
|
a77f08052f | ||
|
|
442b826401 | ||
|
|
0bc75372b5 | ||
|
|
57c7ddb7eb | ||
|
|
a3afefeda9 | ||
|
|
bf90f158ac | ||
|
|
5dbe0d1455 | ||
|
|
7f79c5990b | ||
|
|
bef88ad844 | ||
|
|
839e167c98 | ||
|
|
f40562b68a | ||
|
|
f1830e5f6f | ||
|
|
f38b06abed | ||
|
|
ea4bc88808 | ||
|
|
63e5b4535e | ||
|
|
d913f03e23 | ||
|
|
4c1281754e | ||
|
|
9655fa10f8 | ||
|
|
6ac7b35566 | ||
|
|
62559cd546 | ||
|
|
7b1f1200bc | ||
|
|
39eed856f5 |
49
CHANGELOG.md
49
CHANGELOG.md
@@ -5,6 +5,55 @@ All notable changes to the Daily Notification Plugin will be documented in this
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.1.6] - 2026-02-16
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android**: Alarm set after edit/reschedule now fires. Removed `existingPendingIntent.cancel()` in the "cancel existing alarm before rescheduling" path so the PendingIntent passed to `setAlarmClock` is not cancelled (only `alarmManager.cancel()` is used), fixing no-fire on some devices.
|
||||
|
||||
## [1.1.5] - 2026-02-16
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android**: Rollover work using a `daily_rollover_*` schedule id no longer overwrites the app's schedule row in the DB. `NotifyReceiver` post-schedule update skips the "first enabled notify" fallback when `stableScheduleId` starts with `daily_rollover_`, so the app's reminder (e.g. `daily_timesafari_reminder`) keeps the correct `nextRunAt` after a notification fires.
|
||||
|
||||
### Added
|
||||
|
||||
- **Docs**: `docs/CONSUMING_APP_ANDROID_NOTES.md` — notes for consuming apps on debouncing double `scheduleDailyNotification` calls and debugging alarms that are scheduled but do not fire (logcat with `DailyNotificationReceiver`).
|
||||
|
||||
## [1.1.4] - 2026-02-16
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android**: Re-setting a daily notification (edit/save same time) no longer cancels the alarm and then skips re-scheduling. DB idempotence in `NotifyReceiver.scheduleExactNotification()` now runs only when `!skipPendingIntentIdempotence`, so the app reset flow can re-register the alarm.
|
||||
- **Android**: Static reminder title/body no longer revert to fallback after the first fire. `DailyNotificationWorker.scheduleNextNotification()` now preserves `is_static_reminder` and stable `scheduleId` on rollover so the next occurrence keeps custom text.
|
||||
|
||||
### Added
|
||||
|
||||
- **Android**: `cancelDailyReminder(call)` in `DailyNotificationPlugin.kt` for parity with iOS. Accepts `reminderId` (or `id`, `reminder_id`, `scheduleId`), cancels the AlarmManager alarm for that id, and performs best-effort DB cleanup (`setEnabled` false, `updateRunTimes` null).
|
||||
|
||||
## [1.1.3] - 2026-02-13
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android (Java)**: Java call sites for `NotifyReceiver.scheduleExactNotification()` now pass the 8th parameter `skipPendingIntentIdempotence`, fixing "actual and formal argument lists differ in length" when building consuming apps. Updated `DailyNotificationReceiver.java` and `DailyNotificationWorker.java`.
|
||||
|
||||
## [1.1.2] - 2026-02-13
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android**: Second daily notification not firing after reschedule. After cancel-then-schedule, the idempotence check could still see the cancelled PendingIntent in Android's cache and skip the new schedule. The cancel-then-schedule path now skips PendingIntent-based idempotence so the new alarm is always registered.
|
||||
|
||||
## [1.1.1] - 2026-02-05
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Android**: Target alarm broadcast to app package so receiver is triggered correctly
|
||||
|
||||
### Documentation
|
||||
|
||||
- EMULATOR_GUIDE: prerequisites, API 35, Apple Silicon; build.sh Android-only sync
|
||||
|
||||
## [2.1.0] - 2025-01-02
|
||||
|
||||
### Added
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Daily Notification Plugin
|
||||
|
||||
**Author**: Matthew Raymer
|
||||
**Version**: 1.0.11 (see `package.json` for source of truth)
|
||||
**Version**: 1.2.0 (see `package.json` for source of truth)
|
||||
**Created**: 2025-09-22 09:22:32 UTC
|
||||
**Last Updated**: 2025-12-23 UTC
|
||||
|
||||
|
||||
@@ -76,21 +76,24 @@ class BootReceiver : BroadcastReceiver() {
|
||||
// Reschedule AlarmManager notification
|
||||
val nextRunTime = calculateNextRunTime(schedule)
|
||||
if (nextRunTime > System.currentTimeMillis()) {
|
||||
val (title, body) = ReactivationManager.getTitleBodyForSchedule(db, schedule)
|
||||
?: Pair("Daily Notification", "Your daily update is ready")
|
||||
val config = UserNotificationConfig(
|
||||
enabled = schedule.enabled,
|
||||
schedule = schedule.cron ?: schedule.clockTime ?: "0 9 * * *",
|
||||
title = "Daily Notification",
|
||||
body = "Your daily update is ready",
|
||||
title = title,
|
||||
body = body,
|
||||
sound = true,
|
||||
vibration = true,
|
||||
priority = "normal"
|
||||
)
|
||||
NotifyReceiver.scheduleExactNotification(
|
||||
context,
|
||||
nextRunTime,
|
||||
context,
|
||||
nextRunTime,
|
||||
config,
|
||||
scheduleId = schedule.id,
|
||||
source = ScheduleSource.BOOT_RECOVERY
|
||||
source = ScheduleSource.BOOT_RECOVERY,
|
||||
skipPendingIntentIdempotence = true
|
||||
)
|
||||
Log.i(TAG, "Rescheduled notification for schedule: ${schedule.id}")
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.json.JSONObject
|
||||
* Bridges Capacitor calls to native Android functionality
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
* @version 1.3.0
|
||||
*/
|
||||
@CapacitorPlugin(name = "DailyNotification")
|
||||
open class DailyNotificationPlugin : Plugin() {
|
||||
@@ -706,6 +706,34 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
scheduleDailyNotification(call)
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun cancelDailyReminder(call: PluginCall) {
|
||||
try {
|
||||
val reminderId = call.getString("reminderId")
|
||||
?: call.getString("id")
|
||||
?: call.getString("reminder_id")
|
||||
?: call.getString("scheduleId")
|
||||
if (reminderId.isNullOrBlank()) {
|
||||
call.reject("cancelDailyReminder: missing reminderId")
|
||||
return
|
||||
}
|
||||
NotifyReceiver.cancelNotification(context, scheduleId = reminderId)
|
||||
try {
|
||||
kotlinx.coroutines.runBlocking {
|
||||
val db = getDatabase()
|
||||
db.scheduleDao().setEnabled(reminderId, false)
|
||||
db.scheduleDao().updateRunTimes(reminderId, null, null)
|
||||
}
|
||||
} catch (dbErr: Exception) {
|
||||
Log.w(TAG, "cancelDailyReminder: failed DB update for $reminderId", dbErr)
|
||||
}
|
||||
call.resolve()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "cancelDailyReminder failed", e)
|
||||
call.reject("cancelDailyReminder failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if exact alarms can be scheduled
|
||||
* Helper method for internal use
|
||||
@@ -1081,8 +1109,13 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
val sound = options.getBoolean("sound") ?: true
|
||||
val priority = options.getString("priority") ?: "default"
|
||||
val url = options.getString("url") // Optional URL for prefetch (not used in helper yet)
|
||||
val rolloverIntervalMinutes = try {
|
||||
(options.getInt("rolloverIntervalMinutes") ?: 0).takeIf { it > 0 }
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
Log.i(TAG, "Scheduling daily notification: time=$time, title=$title")
|
||||
Log.i(TAG, "Scheduling daily notification: time=$time, title=$title, rolloverIntervalMinutes=$rolloverIntervalMinutes")
|
||||
|
||||
// Convert HH:mm time to cron expression (daily at specified time)
|
||||
val cronExpression = convertTimeToCron(time)
|
||||
@@ -1109,6 +1142,14 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
Log.i(TAG, "scheduleDailyNotification: No cleanup needed - existing schedule will be updated via upsert: $scheduleId")
|
||||
}
|
||||
|
||||
// Cancel only fetch-related WorkManager jobs so they cannot create a second (UUID) alarm
|
||||
// with fallback or placeholder text. Does not cancel display/dismiss; future fetched-content
|
||||
// flows should use distinct tags so they are not affected.
|
||||
val workCancelled = ScheduleHelper.cancelFetchRelatedWorkManagerJobs(context)
|
||||
if (workCancelled) {
|
||||
Log.i(TAG, "scheduleDailyNotification: Cancelled pending prefetch/fetch WorkManager jobs")
|
||||
}
|
||||
|
||||
val config = UserNotificationConfig(
|
||||
enabled = true,
|
||||
schedule = cronExpression,
|
||||
@@ -1126,7 +1167,8 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
scheduleId,
|
||||
config,
|
||||
time,
|
||||
::calculateNextRunTime
|
||||
::calculateNextRunTime,
|
||||
rolloverIntervalMinutes
|
||||
)
|
||||
|
||||
if (success) {
|
||||
@@ -2072,6 +2114,8 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
val options = call.getObject("options")
|
||||
val timesafariDid = options?.getString("timesafariDid")
|
||||
|
||||
Log.d(TAG, "DNP-CONFIG: Loading config from database: key=$key, timesafariDid=${timesafariDid?.take(20)}...")
|
||||
|
||||
val entity = if (timesafariDid != null) {
|
||||
getDatabase().notificationConfigDao().getConfigByKeyAndDid(key, timesafariDid)
|
||||
} else {
|
||||
@@ -2079,8 +2123,10 @@ open class DailyNotificationPlugin : Plugin() {
|
||||
}
|
||||
|
||||
if (entity != null) {
|
||||
Log.i(TAG, "DNP-CONFIG: Configuration restored from database: key=$key, configType=${entity.configType}, hasValue=${entity.configValue.isNotEmpty()}")
|
||||
call.resolve(configToJson(entity))
|
||||
} else {
|
||||
Log.d(TAG, "DNP-CONFIG: Configuration not found in database: key=$key")
|
||||
call.resolve(JSObject().apply { put("config", null) })
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
@@ -2618,6 +2664,7 @@ object ScheduleHelper {
|
||||
* @param config User notification configuration
|
||||
* @param clockTime Original HH:mm time string
|
||||
* @param calculateNextRunTime Function to calculate next run time from cron expression
|
||||
* @param rolloverIntervalMinutes When > 0, next occurrence is this many minutes after trigger (dev/testing). Null/0 = 24h.
|
||||
* @return true if successful, false otherwise
|
||||
*/
|
||||
suspend fun scheduleDailyNotification(
|
||||
@@ -2626,82 +2673,113 @@ object ScheduleHelper {
|
||||
scheduleId: String,
|
||||
config: UserNotificationConfig,
|
||||
clockTime: String,
|
||||
calculateNextRunTime: (String) -> Long
|
||||
calculateNextRunTime: (String) -> Long,
|
||||
rolloverIntervalMinutes: Int? = null
|
||||
): Boolean {
|
||||
return try {
|
||||
val nextRunTime = calculateNextRunTime(config.schedule)
|
||||
|
||||
// CRITICAL: Cancel any existing alarm for this scheduleId BEFORE scheduling new one
|
||||
// This ensures "one per day" semantics - when updating schedule time, old alarm is canceled
|
||||
// The cleanupExistingNotificationSchedules() above only cancels OTHER schedules, not the current one
|
||||
NotifyReceiver.cancelNotification(context, scheduleId)
|
||||
Log.i("ScheduleHelper", "Cancelled existing alarm for scheduleId=$scheduleId before scheduling new one at $nextRunTime")
|
||||
|
||||
// Schedule AlarmManager notification as static reminder
|
||||
// (doesn't require cached content)
|
||||
// (doesn't require cached content). Skip PendingIntent idempotence: we just cancelled
|
||||
// this scheduleId and Android may still return the cancelled PendingIntent from cache.
|
||||
NotifyReceiver.scheduleExactNotification(
|
||||
context,
|
||||
nextRunTime,
|
||||
context,
|
||||
nextRunTime,
|
||||
config,
|
||||
isStaticReminder = true,
|
||||
reminderId = scheduleId,
|
||||
scheduleId = scheduleId,
|
||||
source = ScheduleSource.INITIAL_SETUP
|
||||
source = ScheduleSource.INITIAL_SETUP,
|
||||
skipPendingIntentIdempotence = true
|
||||
)
|
||||
|
||||
// Always schedule prefetch 2 minutes before notification
|
||||
// (URL is optional - native fetcher will be used if registered)
|
||||
val fetchTime = nextRunTime - (2 * 60 * 1000L) // 2 minutes before
|
||||
val delayMs = fetchTime - System.currentTimeMillis()
|
||||
// Do not enqueue prefetch for static reminders: display is already in the NotifyReceiver
|
||||
// alarm. Prefetch is for "fetch content then show"; for static reminders there is nothing
|
||||
// to fetch. Enqueueing prefetch would cause the worker to use fallback content and
|
||||
// schedule a second alarm via legacy DailyNotificationScheduler, resulting in duplicate
|
||||
// notifications at fire time.
|
||||
|
||||
if (delayMs > 0) {
|
||||
// Schedule delayed prefetch
|
||||
val inputData = Data.Builder()
|
||||
.putLong("scheduled_time", nextRunTime)
|
||||
.putLong("fetch_time", fetchTime)
|
||||
.putInt("retry_count", 0)
|
||||
.putBoolean("immediate", false)
|
||||
.build()
|
||||
|
||||
val workRequest = OneTimeWorkRequestBuilder<DailyNotificationFetchWorker>()
|
||||
.setInitialDelay(delayMs, TimeUnit.MILLISECONDS)
|
||||
.setInputData(inputData)
|
||||
.addTag("prefetch")
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context).enqueue(workRequest)
|
||||
|
||||
Log.i("ScheduleHelper", "Prefetch scheduled: fetchTime=$fetchTime, notificationTime=$nextRunTime, delayMs=$delayMs")
|
||||
} else {
|
||||
// Fetch time is in the past, schedule immediate fetch
|
||||
val inputData = Data.Builder()
|
||||
.putLong("scheduled_time", nextRunTime)
|
||||
.putLong("fetch_time", System.currentTimeMillis())
|
||||
.putInt("retry_count", 0)
|
||||
.putBoolean("immediate", true)
|
||||
.build()
|
||||
|
||||
val workRequest = OneTimeWorkRequestBuilder<DailyNotificationFetchWorker>()
|
||||
.setInputData(inputData)
|
||||
.addTag("prefetch")
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context).enqueue(workRequest)
|
||||
|
||||
Log.i("ScheduleHelper", "Immediate prefetch scheduled: notificationTime=$nextRunTime")
|
||||
}
|
||||
|
||||
// Store schedule in database
|
||||
// Store schedule in database (include rollover interval for dev/testing; survives reboot)
|
||||
val schedule = Schedule(
|
||||
id = scheduleId,
|
||||
kind = "notify",
|
||||
cron = config.schedule,
|
||||
clockTime = clockTime,
|
||||
enabled = true,
|
||||
nextRunAt = nextRunTime
|
||||
nextRunAt = nextRunTime,
|
||||
rolloverIntervalMinutes = rolloverIntervalMinutes
|
||||
)
|
||||
database.scheduleDao().upsert(schedule)
|
||||
|
||||
// Persist title/body for this scheduleId so rollover and post-reboot resolve user content
|
||||
// (see plugin-feedback-android-rollover-double-fire-and-user-content)
|
||||
try {
|
||||
val entity = com.timesafari.dailynotification.entities.NotificationContentEntity(
|
||||
scheduleId,
|
||||
"1.3.0",
|
||||
null,
|
||||
"daily",
|
||||
config.title ?: "Daily Notification",
|
||||
config.body ?: "",
|
||||
nextRunTime,
|
||||
java.time.ZoneId.systemDefault().id
|
||||
)
|
||||
entity.soundEnabled = config.sound ?: true
|
||||
entity.vibrationEnabled = config.vibration ?: true
|
||||
entity.priority = when (config.priority) {
|
||||
"high", "max" -> 2
|
||||
"low", "min" -> -1
|
||||
else -> 0
|
||||
}
|
||||
entity.createdAt = System.currentTimeMillis()
|
||||
entity.updatedAt = System.currentTimeMillis()
|
||||
database.notificationContentDao().insertNotification(entity)
|
||||
Log.d("ScheduleHelper", "Persisted title/body for scheduleId=$scheduleId (rollover/post-reboot)")
|
||||
} catch (e: Exception) {
|
||||
Log.w("ScheduleHelper", "Failed to persist notification content for scheduleId=$scheduleId", e)
|
||||
}
|
||||
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.e("ScheduleHelper", "Failed to schedule daily notification", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocking load of a schedule by id (for use from Java Worker / rollover path).
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getScheduleBlocking(context: Context, scheduleId: String): Schedule? {
|
||||
return kotlinx.coroutines.runBlocking {
|
||||
try {
|
||||
DailyNotificationDatabase.getDatabase(context).scheduleDao().getById(scheduleId)
|
||||
} catch (e: Exception) {
|
||||
Log.w("ScheduleHelper", "getScheduleBlocking failed: $scheduleId", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocking update of schedule next run time (for use from Java Worker after rollover).
|
||||
*/
|
||||
@JvmStatic
|
||||
fun updateScheduleNextRunTimeBlocking(context: Context, scheduleId: String, lastRunAt: Long?, nextRunAt: Long) {
|
||||
kotlinx.coroutines.runBlocking {
|
||||
try {
|
||||
DailyNotificationDatabase.getDatabase(context).scheduleDao().updateRunTimes(scheduleId, lastRunAt, nextRunAt)
|
||||
} catch (e: Exception) {
|
||||
Log.w("ScheduleHelper", "updateScheduleNextRunTimeBlocking failed: $scheduleId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule dual notification (fetch + notify)
|
||||
@@ -2814,9 +2892,30 @@ object ScheduleHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel only WorkManager jobs that can create a second (UUID) alarm for the static-reminder path:
|
||||
* prefetch and daily_notification_fetch. Does not cancel display, dismiss, or maintenance.
|
||||
* Use this when handling scheduleDailyNotification so pending prefetch does not run and create
|
||||
* a duplicate alarm; future fetched-content flows should use distinct tags so they are not affected.
|
||||
*
|
||||
* @param context Application context
|
||||
* @return true if cancellation was successful
|
||||
*/
|
||||
suspend fun cancelFetchRelatedWorkManagerJobs(context: Context): Boolean {
|
||||
return try {
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
workManager.cancelAllWorkByTag("prefetch")
|
||||
workManager.cancelAllWorkByTag("daily_notification_fetch")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.w("ScheduleHelper", "Failed to cancel fetch-related WorkManager jobs", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all WorkManager jobs by tags
|
||||
*
|
||||
*
|
||||
* @param context Application context
|
||||
* @return true if cancellation was successful
|
||||
*/
|
||||
|
||||
@@ -107,9 +107,11 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
|
||||
// Create unique work name based on notification ID to prevent duplicates
|
||||
// WorkManager will automatically skip if work with this name already exists
|
||||
String workName = "display_" + notificationId;
|
||||
|
||||
|
||||
// Extract static reminder extras from intent if present
|
||||
// Static reminders have title/body in Intent extras, not in storage
|
||||
// Static reminders have title/body in Intent extras, not in storage.
|
||||
// Do NOT access DB on main thread here (Room disallows it); Worker will resolve
|
||||
// missing title/body by schedule_id on a background thread (see plugin-feedback-android-rollover-double-fire).
|
||||
boolean isStaticReminder = intent.getBooleanExtra("is_static_reminder", false);
|
||||
String title = intent.getStringExtra("title");
|
||||
String body = intent.getStringExtra("body");
|
||||
@@ -119,13 +121,17 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
|
||||
if (priority == null) {
|
||||
priority = "normal";
|
||||
}
|
||||
|
||||
String scheduleId = intent.getStringExtra("schedule_id");
|
||||
|
||||
Data.Builder dataBuilder = new Data.Builder()
|
||||
.putString("notification_id", notificationId)
|
||||
.putString("action", "display")
|
||||
.putBoolean("is_static_reminder", isStaticReminder);
|
||||
|
||||
// Add static reminder data if present
|
||||
if (scheduleId != null && !scheduleId.isEmpty()) {
|
||||
dataBuilder.putString("schedule_id", scheduleId);
|
||||
}
|
||||
|
||||
// Add static reminder data when present (from Intent; Worker resolves from DB by schedule_id if missing)
|
||||
if (isStaticReminder && title != null && body != null) {
|
||||
dataBuilder.putString("title", title)
|
||||
.putString("body", body)
|
||||
@@ -134,7 +140,7 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
|
||||
.putString("priority", priority);
|
||||
Log.d(TAG, "DN|WORK_ENQUEUE static_reminder id=" + notificationId);
|
||||
}
|
||||
|
||||
|
||||
Data inputData = dataBuilder.build();
|
||||
|
||||
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(DailyNotificationWorker.class)
|
||||
@@ -195,7 +201,7 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
|
||||
Log.e(TAG, "DN|WORK_ENQUEUE_ERR dismiss=" + notificationId + " err=" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle notification intent
|
||||
*
|
||||
@@ -445,7 +451,8 @@ public class DailyNotificationReceiver extends BroadcastReceiver {
|
||||
false, // isStaticReminder
|
||||
null, // reminderId
|
||||
scheduleId,
|
||||
com.timesafari.dailynotification.ScheduleSource.ROLLOVER_ON_FIRE
|
||||
com.timesafari.dailynotification.ScheduleSource.ROLLOVER_ON_FIRE,
|
||||
false // skipPendingIntentIdempotence – rollover path does not skip
|
||||
);
|
||||
|
||||
Log.i(TAG, "Next notification scheduled via centralized function: scheduleId=" + scheduleId);
|
||||
|
||||
@@ -154,8 +154,9 @@ public class DailyNotificationScheduler {
|
||||
cancelNotification(duplicateId);
|
||||
}
|
||||
|
||||
// Create intent for the notification
|
||||
// Create intent for the notification; setPackage ensures AlarmManager delivery on all OEMs
|
||||
Intent intent = new Intent(context, DailyNotificationReceiver.class);
|
||||
intent.setPackage(context.getPackageName());
|
||||
intent.setAction(com.timesafari.dailynotification.DailyNotificationConstants.ACTION_NOTIFICATION);
|
||||
intent.putExtra(com.timesafari.dailynotification.DailyNotificationConstants.EXTRA_NOTIFICATION_ID, content.getId());
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ public class DailyNotificationWorker extends Worker {
|
||||
NotificationContent content;
|
||||
|
||||
if (isStaticReminder) {
|
||||
// Static reminder: create NotificationContent from input data
|
||||
// Static reminder: create NotificationContent from input data (or resolve from DB by schedule_id)
|
||||
String title = inputData.getString("title");
|
||||
String body = inputData.getString("body");
|
||||
boolean sound = inputData.getBoolean("sound", true);
|
||||
@@ -142,7 +142,18 @@ public class DailyNotificationWorker extends Worker {
|
||||
if (priority == null) {
|
||||
priority = "normal";
|
||||
}
|
||||
|
||||
// Post-reboot/rollover: Intent may lack title/body; resolve from DB by canonical schedule_id
|
||||
String scheduleId = inputData.getString("schedule_id");
|
||||
if ((title == null || title.isEmpty() || body == null || body.isEmpty()) && scheduleId != null) {
|
||||
NotificationContent canonical = getContentByScheduleId(scheduleId);
|
||||
if (canonical != null && canonical.getTitle() != null && canonical.getBody() != null) {
|
||||
title = canonical.getTitle();
|
||||
body = canonical.getBody();
|
||||
sound = canonical.isSound();
|
||||
priority = canonical.getPriority() != null ? canonical.getPriority() : "normal";
|
||||
Log.d(TAG, "DN|DISPLAY_STATIC_REMINDER_FROM_DB id=" + notificationId + " schedule_id=" + scheduleId);
|
||||
}
|
||||
}
|
||||
if (title == null || body == null) {
|
||||
Log.w(TAG, "DN|DISPLAY_SKIP static_reminder_missing_data id=" + notificationId);
|
||||
return Result.success();
|
||||
@@ -160,25 +171,35 @@ public class DailyNotificationWorker extends Worker {
|
||||
|
||||
Log.d(TAG, "DN|DISPLAY_STATIC_REMINDER id=" + notificationId + " title=" + title);
|
||||
} else {
|
||||
// Regular notification: load from storage
|
||||
// Prefer Room storage; fallback to legacy SharedPreferences storage
|
||||
// Regular notification: load from storage (by notification_id, then by schedule_id for rollover/user content)
|
||||
content = getContentFromRoomOrLegacy(notificationId);
|
||||
|
||||
if (content == null) {
|
||||
// Content not found - likely removed during deduplication or cleanup
|
||||
// Return success instead of failure to prevent retries for intentionally removed notifications
|
||||
Log.w(TAG, "DN|DISPLAY_SKIP content_not_found id=" + notificationId + " (likely removed during deduplication)");
|
||||
return Result.success(); // Success prevents retry loops for removed notifications
|
||||
}
|
||||
|
||||
// Check if notification is ready to display
|
||||
if (!content.isReadyToDisplay()) {
|
||||
Log.d(TAG, "DN|DISPLAY_SKIP not_ready id=" + notificationId);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
// JIT Freshness Re-check (Soft TTL) - skip for static reminders
|
||||
content = performJITFreshnessCheck(content);
|
||||
// Rollover/notify_* runs: prefer canonical reminder content by schedule_id so user text is shown
|
||||
String scheduleId = inputData.getString("schedule_id");
|
||||
if (scheduleId != null && (content == null || content.getTitle() == null || content.getTitle().isEmpty()
|
||||
|| content.getBody() == null || content.getBody().isEmpty())) {
|
||||
NotificationContent canonical = getContentByScheduleId(scheduleId);
|
||||
if (canonical != null && canonical.getTitle() != null && canonical.getBody() != null) {
|
||||
content = canonical;
|
||||
content.setId(notificationId); // keep run id for display/dismiss
|
||||
Log.d(TAG, "DN|DISPLAY_USE_CANONICAL id=" + notificationId + " schedule_id=" + scheduleId);
|
||||
}
|
||||
}
|
||||
if (content == null) {
|
||||
Log.w(TAG, "DN|DISPLAY_SKIP content_not_found id=" + notificationId + " (likely removed during deduplication)");
|
||||
return Result.success();
|
||||
}
|
||||
if (!content.isReadyToDisplay()) {
|
||||
Log.d(TAG, "DN|DISPLAY_SKIP not_ready id=" + notificationId);
|
||||
return Result.success();
|
||||
}
|
||||
// JIT Freshness Re-check (Soft TTL) - skip when content has title/body from Room
|
||||
boolean hasTitleBody = content.getTitle() != null && !content.getTitle().isEmpty()
|
||||
&& content.getBody() != null && !content.getBody().isEmpty();
|
||||
if (!hasTitleBody) {
|
||||
content = performJITFreshnessCheck(content);
|
||||
} else {
|
||||
Log.d(TAG, "DN|DISPLAY_USE_ROOM_CONTENT id=" + notificationId + " (skip JIT)");
|
||||
}
|
||||
}
|
||||
|
||||
// Display the notification
|
||||
@@ -514,8 +535,33 @@ public class DailyNotificationWorker extends Worker {
|
||||
try {
|
||||
Log.d(TAG, "DN|RESCHEDULE_START id=" + content.getId());
|
||||
|
||||
// Calculate next occurrence using DST-safe ZonedDateTime
|
||||
long nextScheduledTime = calculateNextScheduledTime(content.getScheduledTime());
|
||||
// Resolve schedule_id first so we can load rollover interval from DB
|
||||
Data inputDataForSchedule = getInputData();
|
||||
boolean preserveStaticReminder = inputDataForSchedule.getBoolean("is_static_reminder", false);
|
||||
String scheduleIdForRollover = inputDataForSchedule.getString("schedule_id");
|
||||
if (scheduleIdForRollover == null || scheduleIdForRollover.isEmpty()) {
|
||||
String notificationId = content.getId();
|
||||
if (preserveStaticReminder && notificationId != null && !notificationId.isEmpty()) {
|
||||
scheduleIdForRollover = notificationId;
|
||||
} else if (notificationId != null && notificationId.startsWith("daily_")) {
|
||||
scheduleIdForRollover = notificationId;
|
||||
}
|
||||
}
|
||||
Integer rolloverMinutes = null;
|
||||
if (scheduleIdForRollover != null && !scheduleIdForRollover.isEmpty()) {
|
||||
com.timesafari.dailynotification.Schedule s = com.timesafari.dailynotification.ScheduleHelper.getScheduleBlocking(getApplicationContext(), scheduleIdForRollover);
|
||||
if (s != null && s.getRolloverIntervalMinutes() != null && s.getRolloverIntervalMinutes() > 0) {
|
||||
rolloverMinutes = s.getRolloverIntervalMinutes();
|
||||
Log.d(TAG, "DN|ROLLOVER_INTERVAL scheduleId=" + scheduleIdForRollover + " minutes=" + rolloverMinutes);
|
||||
}
|
||||
}
|
||||
long nextScheduledTime;
|
||||
if (rolloverMinutes != null && rolloverMinutes > 0) {
|
||||
nextScheduledTime = addMinutesToTime(content.getScheduledTime(), rolloverMinutes);
|
||||
Log.d(TAG, "DN|ROLLOVER_NEXT using_interval_minutes=" + rolloverMinutes + " next=" + nextScheduledTime);
|
||||
} else {
|
||||
nextScheduledTime = calculateNextScheduledTime(content.getScheduledTime());
|
||||
}
|
||||
|
||||
// Check for existing notification at the same time to prevent duplicates
|
||||
DailyNotificationStorage legacyStorage = new DailyNotificationStorage(getApplicationContext());
|
||||
@@ -540,32 +586,30 @@ public class DailyNotificationWorker extends Worker {
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract scheduleId from notificationId pattern or use fallback
|
||||
// Notification IDs are often "daily_${scheduleId}"
|
||||
String scheduleId = null;
|
||||
String cronExpression = null;
|
||||
|
||||
// Try to extract scheduleId from notificationId (e.g., "daily_1764578136269")
|
||||
String notificationId = content.getId();
|
||||
if (notificationId != null && notificationId.startsWith("daily_")) {
|
||||
scheduleId = notificationId; // Use notificationId as scheduleId
|
||||
} else {
|
||||
String scheduleId = scheduleIdForRollover;
|
||||
if (scheduleId == null || scheduleId.isEmpty()) {
|
||||
scheduleId = "daily_rollover_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
// Calculate cron from current scheduled time (extract hour:minute)
|
||||
try {
|
||||
java.util.Calendar cal = java.util.Calendar.getInstance();
|
||||
cal.setTimeInMillis(content.getScheduledTime());
|
||||
int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
|
||||
int minute = cal.get(java.util.Calendar.MINUTE);
|
||||
cronExpression = String.format("%d %d * * *", minute, hour);
|
||||
|
||||
// Recalculate next run time from cron (tomorrow at same time)
|
||||
nextScheduledTime = calculateNextRunTimeFromCron(cronExpression);
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Failed to calculate cron from scheduled time, using default", e);
|
||||
cronExpression = "0 9 * * *"; // Default to 9 AM
|
||||
// When using rollover interval, next time already set; otherwise compute from cron (tomorrow same time)
|
||||
if (rolloverMinutes == null || rolloverMinutes <= 0) {
|
||||
try {
|
||||
java.util.Calendar cal = java.util.Calendar.getInstance();
|
||||
cal.setTimeInMillis(content.getScheduledTime());
|
||||
int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
|
||||
int minute = cal.get(java.util.Calendar.MINUTE);
|
||||
cronExpression = String.format("%d %d * * *", minute, hour);
|
||||
nextScheduledTime = calculateNextRunTimeFromCron(cronExpression);
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Failed to calculate cron from scheduled time, using default", e);
|
||||
cronExpression = "0 9 * * *";
|
||||
}
|
||||
} else {
|
||||
cronExpression = String.format("%d %d * * *",
|
||||
java.util.Calendar.getInstance().get(java.util.Calendar.MINUTE),
|
||||
java.util.Calendar.getInstance().get(java.util.Calendar.HOUR_OF_DAY));
|
||||
}
|
||||
|
||||
// Create config for next notification
|
||||
@@ -581,48 +625,50 @@ public class DailyNotificationWorker extends Worker {
|
||||
);
|
||||
|
||||
// Use centralized scheduling function with ROLLOVER_ON_FIRE source
|
||||
Log.d(TAG, "DN|ROLLOVER next=" + nextScheduledTime + " scheduleId=" + scheduleId + " static=" + preserveStaticReminder);
|
||||
com.timesafari.dailynotification.NotifyReceiver.scheduleExactNotification(
|
||||
getApplicationContext(),
|
||||
nextScheduledTime,
|
||||
config,
|
||||
false, // isStaticReminder
|
||||
null, // reminderId
|
||||
preserveStaticReminder, // isStaticReminder – preserve so next run keeps title/body
|
||||
preserveStaticReminder ? scheduleId : null, // reminderId
|
||||
scheduleId,
|
||||
com.timesafari.dailynotification.ScheduleSource.ROLLOVER_ON_FIRE
|
||||
com.timesafari.dailynotification.ScheduleSource.ROLLOVER_ON_FIRE,
|
||||
false // skipPendingIntentIdempotence – rollover path does not skip
|
||||
);
|
||||
|
||||
if (scheduleId != null && !scheduleId.startsWith("daily_rollover_")) {
|
||||
com.timesafari.dailynotification.ScheduleHelper.updateScheduleNextRunTimeBlocking(
|
||||
getApplicationContext(), scheduleId, content.getScheduledTime(), nextScheduledTime);
|
||||
}
|
||||
// Log next scheduled time in readable format
|
||||
String nextTimeStr = formatScheduledTime(nextScheduledTime);
|
||||
Log.i(TAG, "DN|RESCHEDULE_OK id=" + content.getId() + " next=" + nextTimeStr + " scheduleId=" + scheduleId);
|
||||
|
||||
// Schedule background fetch for next notification (5 minutes before scheduled time)
|
||||
try {
|
||||
DailyNotificationStorage storageForFetcher = new DailyNotificationStorage(getApplicationContext());
|
||||
DailyNotificationStorageRoom roomStorageForFetcher = new DailyNotificationStorageRoom(getApplicationContext());
|
||||
DailyNotificationFetcher fetcher = new DailyNotificationFetcher(
|
||||
getApplicationContext(),
|
||||
storageForFetcher,
|
||||
roomStorageForFetcher
|
||||
);
|
||||
|
||||
// Calculate fetch time (5 minutes before notification)
|
||||
long fetchTime = nextScheduledTime - TimeUnit.MINUTES.toMillis(5);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
if (fetchTime > currentTime) {
|
||||
fetcher.scheduleFetch(fetchTime);
|
||||
Log.i(TAG, "DN|RESCHEDULE_PREFETCH_SCHEDULED id=" + content.getId() +
|
||||
" next_fetch=" + fetchTime +
|
||||
" next_notification=" + nextScheduledTime);
|
||||
} else {
|
||||
Log.w(TAG, "DN|RESCHEDULE_PREFETCH_PAST id=" + content.getId() +
|
||||
" fetch_time=" + fetchTime +
|
||||
" current=" + currentTime);
|
||||
fetcher.scheduleImmediateFetch();
|
||||
// Do not schedule prefetch for static reminders (single NotifyReceiver alarm is enough; avoids second alarm)
|
||||
if (preserveStaticReminder) {
|
||||
Log.d(TAG, "DN|RESCHEDULE_SKIP_PREFETCH static_reminder scheduleId=" + scheduleId);
|
||||
} else {
|
||||
// Schedule background fetch for next notification (5 minutes before scheduled time)
|
||||
try {
|
||||
DailyNotificationStorage storageForFetcher = new DailyNotificationStorage(getApplicationContext());
|
||||
DailyNotificationStorageRoom roomStorageForFetcher = new DailyNotificationStorageRoom(getApplicationContext());
|
||||
DailyNotificationFetcher fetcher = new DailyNotificationFetcher(
|
||||
getApplicationContext(),
|
||||
storageForFetcher,
|
||||
roomStorageForFetcher
|
||||
);
|
||||
long fetchTime = nextScheduledTime - TimeUnit.MINUTES.toMillis(5);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (fetchTime > currentTime) {
|
||||
fetcher.scheduleFetch(fetchTime);
|
||||
Log.i(TAG, "DN|RESCHEDULE_PREFETCH_SCHEDULED id=" + content.getId() + " next_fetch=" + fetchTime + " next_notification=" + nextScheduledTime);
|
||||
} else {
|
||||
Log.w(TAG, "DN|RESCHEDULE_PREFETCH_PAST id=" + content.getId() + " fetch_time=" + fetchTime + " current=" + currentTime);
|
||||
fetcher.scheduleImmediateFetch();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "DN|RESCHEDULE_PREFETCH_ERR id=" + content.getId() + " error scheduling prefetch", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "DN|RESCHEDULE_PREFETCH_ERR id=" + content.getId() +
|
||||
" error scheduling prefetch", e);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -632,6 +678,28 @@ public class DailyNotificationWorker extends Worker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load notification content by canonical schedule id (for static reminder / rollover user text).
|
||||
* Tries id then "daily_" + id to match getTitleBodyForSchedule / BootReceiver.
|
||||
*/
|
||||
private NotificationContent getContentByScheduleId(String scheduleId) {
|
||||
if (scheduleId == null || scheduleId.isEmpty()) return null;
|
||||
try {
|
||||
com.timesafari.dailynotification.DailyNotificationDatabase db =
|
||||
com.timesafari.dailynotification.DailyNotificationDatabase.getInstance(getApplicationContext());
|
||||
NotificationContentEntity entity = db.notificationContentDao().getNotificationById(scheduleId);
|
||||
if (entity == null) {
|
||||
entity = db.notificationContentDao().getNotificationById("daily_" + scheduleId);
|
||||
}
|
||||
if (entity != null) {
|
||||
return mapEntityToContent(entity);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "DN|CANONICAL_READ_FAIL schedule_id=" + scheduleId + " err=" + t.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to load content from Room; fallback to legacy storage
|
||||
*/
|
||||
@@ -688,7 +756,7 @@ public class DailyNotificationWorker extends Worker {
|
||||
DailyNotificationStorageRoom room = new DailyNotificationStorageRoom(getApplicationContext());
|
||||
NotificationContentEntity entity = new NotificationContentEntity(
|
||||
content.getId() != null ? content.getId() : java.util.UUID.randomUUID().toString(),
|
||||
"1.0.0",
|
||||
"1.2.1",
|
||||
null,
|
||||
"daily",
|
||||
content.getTitle(),
|
||||
@@ -725,6 +793,21 @@ public class DailyNotificationWorker extends Worker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add minutes to a timestamp (DST-safe via Calendar).
|
||||
* Used for rollover interval (e.g. 10 minutes for testing).
|
||||
*/
|
||||
private long addMinutesToTime(long timeMillis, int minutes) {
|
||||
try {
|
||||
java.util.Calendar cal = java.util.Calendar.getInstance();
|
||||
cal.setTimeInMillis(timeMillis);
|
||||
cal.add(java.util.Calendar.MINUTE, minutes);
|
||||
return cal.getTimeInMillis();
|
||||
} catch (Exception e) {
|
||||
return timeMillis + (minutes * 60 * 1000L);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate next scheduled time with DST-safe handling
|
||||
*
|
||||
|
||||
@@ -47,7 +47,9 @@ data class Schedule(
|
||||
val nextRunAt: Long? = null,
|
||||
val jitterMs: Int = 0,
|
||||
val backoffPolicy: String = "exp",
|
||||
val stateJson: String? = null
|
||||
val stateJson: String? = null,
|
||||
/** When > 0, next occurrence is this many minutes after current trigger (dev/testing). Null or 0 = 24h. */
|
||||
val rolloverIntervalMinutes: Int? = null
|
||||
)
|
||||
|
||||
@Entity(tableName = "callbacks")
|
||||
@@ -83,7 +85,7 @@ data class History(
|
||||
NotificationDeliveryEntity::class,
|
||||
NotificationConfigEntity::class
|
||||
],
|
||||
version = 2, // Incremented for unified schema
|
||||
version = 3, // 3: add rollover_interval_minutes to schedules
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
@@ -118,7 +120,7 @@ abstract class DailyNotificationDatabase : RoomDatabase() {
|
||||
DailyNotificationDatabase::class.java,
|
||||
DATABASE_NAME
|
||||
)
|
||||
.addMigrations(MIGRATION_1_2) // Migration from Kotlin-only to unified
|
||||
.addMigrations(MIGRATION_1_2, MIGRATION_2_3) // 1->2: unified; 2->3: rollover_interval_minutes
|
||||
.addCallback(roomCallback)
|
||||
.build()
|
||||
INSTANCE = instance
|
||||
@@ -266,6 +268,15 @@ abstract class DailyNotificationDatabase : RoomDatabase() {
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migration from version 2 to 3: add rollover_interval_minutes to schedules
|
||||
*/
|
||||
val MIGRATION_2_3 = object : Migration(2, 3) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE schedules ADD COLUMN rollover_interval_minutes INTEGER")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.json.JSONObject
|
||||
* Implements exponential backoff and network constraints
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
* @version 1.3.0
|
||||
*/
|
||||
class FetchWorker(
|
||||
appContext: Context,
|
||||
@@ -205,7 +205,7 @@ class FetchWorker(
|
||||
|
||||
val entity = com.timesafari.dailynotification.entities.NotificationContentEntity(
|
||||
notificationId,
|
||||
"1.0.2", // Plugin version
|
||||
"1.3.0", // Plugin version
|
||||
null, // timesafariDid - can be set if available
|
||||
"daily",
|
||||
title,
|
||||
@@ -301,7 +301,7 @@ class FetchWorker(
|
||||
"timestamp": ${System.currentTimeMillis()},
|
||||
"content": "Daily notification content",
|
||||
"source": "mock_generator",
|
||||
"version": "1.1.0"
|
||||
"version": "1.3.0"
|
||||
}
|
||||
""".trimIndent()
|
||||
return mockData.toByteArray()
|
||||
|
||||
@@ -21,7 +21,7 @@ import kotlinx.coroutines.runBlocking
|
||||
* Implements TTL-at-fire logic and notification delivery
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
* @version 1.3.0
|
||||
*/
|
||||
/**
|
||||
* Source of schedule request - tracks which code path triggered scheduling
|
||||
@@ -122,103 +122,113 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
* @param reminderId Optional reminder ID for tracking (used as scheduleId if provided)
|
||||
* @param scheduleId Stable identifier for the schedule (used for requestCode stability)
|
||||
* @param source Source of the scheduling request (for debugging duplicate alarms)
|
||||
* @param skipPendingIntentIdempotence If true, skip PendingIntent-based idempotence checks.
|
||||
* Use when the caller has just cancelled this scheduleId (cancel-then-schedule path).
|
||||
* Android may still return the cancelled PendingIntent from cache briefly, which would
|
||||
* incorrectly cause the new schedule to be skipped.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun scheduleExactNotification(
|
||||
context: Context,
|
||||
context: Context,
|
||||
triggerAtMillis: Long,
|
||||
config: UserNotificationConfig,
|
||||
isStaticReminder: Boolean = false,
|
||||
reminderId: String? = null,
|
||||
scheduleId: String? = null,
|
||||
source: ScheduleSource = ScheduleSource.MANUAL_RESCHEDULE
|
||||
source: ScheduleSource = ScheduleSource.MANUAL_RESCHEDULE,
|
||||
skipPendingIntentIdempotence: Boolean = false
|
||||
) {
|
||||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
|
||||
|
||||
// Generate stable scheduleId - prefer provided scheduleId, then reminderId, then generate from time
|
||||
// This ensures same schedule always uses same ID for idempotence checks
|
||||
val stableScheduleId = scheduleId ?: reminderId ?: "daily_${triggerAtMillis}"
|
||||
|
||||
|
||||
// Generate notification ID (use reminderId if provided, otherwise generate from trigger time)
|
||||
val notificationId = reminderId ?: "notify_${triggerAtMillis}"
|
||||
|
||||
// IDEMPOTENCE CHECK: Verify no existing alarm for this trigger time before scheduling
|
||||
// This prevents duplicate alarms when multiple scheduling paths race
|
||||
// Strategy: Check both by scheduleId (stable) and by trigger time (catches different scheduleIds for same time)
|
||||
|
||||
val requestCode = getRequestCode(stableScheduleId)
|
||||
val checkIntent = Intent(context, com.timesafari.dailynotification.DailyNotificationReceiver::class.java).apply {
|
||||
setPackage(context.packageName)
|
||||
action = "com.timesafari.daily.NOTIFICATION"
|
||||
}
|
||||
|
||||
// Check 1: Same scheduleId (stable requestCode) - most reliable
|
||||
var existingPendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
checkIntent,
|
||||
PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
// Check 2: If no match by scheduleId, check by trigger time (within 1 minute tolerance)
|
||||
// This catches cases where different scheduleIds are used for the same time
|
||||
// Try a range of request codes around the trigger time
|
||||
if (existingPendingIntent == null) {
|
||||
val timeBasedRequestCode = getRequestCodeFromTime(triggerAtMillis)
|
||||
existingPendingIntent = PendingIntent.getBroadcast(
|
||||
|
||||
// IDEMPOTENCE CHECK: Verify no existing alarm for this trigger time before scheduling.
|
||||
// Skip PendingIntent checks when caller just cancelled this schedule (Android may still
|
||||
// return the cancelled PendingIntent from cache and cause the new schedule to be skipped).
|
||||
if (!skipPendingIntentIdempotence) {
|
||||
// Check 1: Same scheduleId (stable requestCode) - most reliable
|
||||
var existingPendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
timeBasedRequestCode,
|
||||
requestCode,
|
||||
checkIntent,
|
||||
PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
// Check 3: Also check if AlarmManager already has an alarm for this exact time
|
||||
// This is a fallback for when PendingIntent checks fail but alarm still exists
|
||||
// We check the next alarm clock time (Android 5.0+)
|
||||
if (existingPendingIntent == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
val nextAlarm = alarmManager.nextAlarmClock
|
||||
if (nextAlarm != null) {
|
||||
val nextAlarmTime = nextAlarm.triggerTime
|
||||
val timeDiff = Math.abs(nextAlarmTime - triggerAtMillis)
|
||||
// If there's an alarm within 1 minute of our target time, consider it a duplicate
|
||||
if (timeDiff < 60000) {
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
.format(java.util.Date(triggerAtMillis))
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule: id=$stableScheduleId, nextRun=$triggerTimeStr, source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing alarm found in AlarmManager at $nextAlarmTime (diff=${timeDiff}ms) - alarm already scheduled")
|
||||
return
|
||||
}
|
||||
|
||||
// Check 2: If no match by scheduleId, check by trigger time (within 1 minute tolerance)
|
||||
if (existingPendingIntent == null) {
|
||||
val timeBasedRequestCode = getRequestCodeFromTime(triggerAtMillis)
|
||||
existingPendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
timeBasedRequestCode,
|
||||
checkIntent,
|
||||
PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (existingPendingIntent != null) {
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
.format(java.util.Date(triggerAtMillis))
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule: id=$stableScheduleId, nextRun=$triggerTimeStr, source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing PendingIntent found for requestCode=$requestCode - alarm already scheduled")
|
||||
return
|
||||
}
|
||||
|
||||
// DB-LEVEL IDEMPOTENCE CHECK: Verify no existing schedule for this scheduleId and nextRun
|
||||
// This prevents logical duplicates before even hitting AlarmManager
|
||||
try {
|
||||
runBlocking {
|
||||
val db = DailyNotificationDatabase.getDatabase(context)
|
||||
val existingSchedule = db.scheduleDao().getById(stableScheduleId)
|
||||
|
||||
if (existingSchedule != null && existingSchedule.nextRunAt != null) {
|
||||
val timeDiff = Math.abs(existingSchedule.nextRunAt - triggerAtMillis)
|
||||
// If we already have a schedule for this ID with the same nextRun (within 1 minute), skip
|
||||
|
||||
// Check 3: AlarmManager next alarm (Android 5.0+)
|
||||
if (existingPendingIntent == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
val nextAlarm = alarmManager.nextAlarmClock
|
||||
if (nextAlarm != null) {
|
||||
val nextAlarmTime = nextAlarm.triggerTime
|
||||
val timeDiff = Math.abs(nextAlarmTime - triggerAtMillis)
|
||||
if (timeDiff < 60000) {
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
.format(java.util.Date(triggerAtMillis))
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule for id=$stableScheduleId at $triggerTimeStr from source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing schedule found in DB: nextRunAt=${existingSchedule.nextRunAt}, diff=${timeDiff}ms")
|
||||
return@runBlocking
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule: id=$stableScheduleId, nextRun=$triggerTimeStr, source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing alarm found in AlarmManager at $nextAlarmTime (diff=${timeDiff}ms) - alarm already scheduled")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(SCHEDULE_TAG, "DB idempotence check failed, continuing with schedule: $stableScheduleId", e)
|
||||
|
||||
if (existingPendingIntent != null) {
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
.format(java.util.Date(triggerAtMillis))
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule: id=$stableScheduleId, nextRun=$triggerTimeStr, source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing PendingIntent found for requestCode=$requestCode - alarm already scheduled")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
Log.d(SCHEDULE_TAG, "Skipping PendingIntent idempotence (caller just cancelled scheduleId=$stableScheduleId)")
|
||||
}
|
||||
|
||||
// DB-LEVEL IDEMPOTENCE CHECK: Verify no existing schedule for this scheduleId and nextRun
|
||||
// When skipPendingIntentIdempotence is true (e.g. "re-set" flow), skip this check so we don't
|
||||
// cancel the alarm and then skip re-scheduling, resulting in no alarm.
|
||||
if (!skipPendingIntentIdempotence) {
|
||||
try {
|
||||
runBlocking {
|
||||
val db = DailyNotificationDatabase.getDatabase(context)
|
||||
val existingSchedule = db.scheduleDao().getById(stableScheduleId)
|
||||
|
||||
if (existingSchedule != null && existingSchedule.nextRunAt != null) {
|
||||
val timeDiff = Math.abs(existingSchedule.nextRunAt - triggerAtMillis)
|
||||
// If we already have a schedule for this ID with the same nextRun (within 1 minute), skip
|
||||
if (timeDiff < 60000) {
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
.format(java.util.Date(triggerAtMillis))
|
||||
Log.w(SCHEDULE_TAG, "Skipping duplicate schedule for id=$stableScheduleId at $triggerTimeStr from source=$source")
|
||||
Log.w(SCHEDULE_TAG, "Existing schedule found in DB: nextRunAt=${existingSchedule.nextRunAt}, diff=${timeDiff}ms")
|
||||
return@runBlocking
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(SCHEDULE_TAG, "DB idempotence check failed, continuing with schedule: $stableScheduleId", e)
|
||||
}
|
||||
} else {
|
||||
Log.d(SCHEDULE_TAG, "Skipping DB idempotence (skipPendingIntentIdempotence=true) for scheduleId=$stableScheduleId")
|
||||
}
|
||||
|
||||
val triggerTimeStr = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)
|
||||
@@ -241,7 +251,7 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
val roomStorage = com.timesafari.dailynotification.storage.DailyNotificationStorageRoom(context)
|
||||
val entity = com.timesafari.dailynotification.entities.NotificationContentEntity(
|
||||
notificationId,
|
||||
"1.0.2", // Plugin version
|
||||
"1.3.0", // Plugin version
|
||||
null, // timesafariDid - can be set if available
|
||||
"daily",
|
||||
config.title,
|
||||
@@ -270,8 +280,10 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
// FIX: Use DailyNotificationReceiver (registered in manifest) instead of NotifyReceiver
|
||||
// FIX: Set action to match manifest registration
|
||||
// FIX: Set action to match manifest registration; setPackage() ensures AlarmManager
|
||||
// delivery reaches this app on all OEMs (see daily-notification-plugin-android-receiver-issue)
|
||||
val intent = Intent(context, com.timesafari.dailynotification.DailyNotificationReceiver::class.java).apply {
|
||||
setPackage(context.packageName)
|
||||
action = "com.timesafari.daily.NOTIFICATION" // Must match manifest intent-filter action
|
||||
putExtra("notification_id", notificationId) // DailyNotificationReceiver expects this extra
|
||||
putExtra("schedule_id", stableScheduleId) // Add stable scheduleId for tracking
|
||||
@@ -309,7 +321,8 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
if (existingPendingIntent != null) {
|
||||
Log.w(SCHEDULE_TAG, "Cancelling existing alarm before rescheduling: requestCode=$requestCode, scheduleId=$stableScheduleId, source=$source")
|
||||
alarmManager.cancel(existingPendingIntent)
|
||||
existingPendingIntent.cancel()
|
||||
// Do not call existingPendingIntent.cancel(): the cached PendingIntent may be the same
|
||||
// object we pass to setAlarmClock below; cancelling it can prevent the new alarm from firing.
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(SCHEDULE_TAG, "Failed to cancel existing alarm before scheduling: $stableScheduleId", e)
|
||||
@@ -396,6 +409,63 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
)
|
||||
Log.i(TAG, "Inexact alarm scheduled (fallback): triggerAt=$triggerAtMillis, requestCode=$requestCode")
|
||||
}
|
||||
|
||||
// Update database schedule with new nextRunAt so getNotificationStatus() returns correct value
|
||||
// This is critical for rollover scenarios where the UI needs to show the updated time
|
||||
// Strategy: Find existing enabled notify schedule and update it (there should only be one)
|
||||
// This ensures getNotificationStatus() finds the updated schedule, not a stale one
|
||||
try {
|
||||
runBlocking {
|
||||
val db = DailyNotificationDatabase.getDatabase(context)
|
||||
|
||||
// First, try to find schedule by the provided stableScheduleId
|
||||
var scheduleToUpdate = db.scheduleDao().getById(stableScheduleId)
|
||||
|
||||
// If not found by ID, only use "first enabled notify" fallback when this is NOT
|
||||
// a rollover id (daily_rollover_*). Rollover work may use a different notification_id
|
||||
// (e.g. from recovery); updating the app's schedule row here would overwrite
|
||||
// nextRunAt with the rollover time and can leave the app's alarm in a bad state.
|
||||
if (scheduleToUpdate == null && !stableScheduleId.startsWith("daily_rollover_")) {
|
||||
val allSchedules = db.scheduleDao().getAll()
|
||||
scheduleToUpdate = allSchedules.firstOrNull { it.kind == "notify" && it.enabled }
|
||||
}
|
||||
|
||||
// Calculate cron expression from trigger time (HH:mm format)
|
||||
val calendar = java.util.Calendar.getInstance().apply {
|
||||
timeInMillis = triggerAtMillis
|
||||
}
|
||||
val hour = calendar.get(java.util.Calendar.HOUR_OF_DAY)
|
||||
val minute = calendar.get(java.util.Calendar.MINUTE)
|
||||
val cronExpression = "${minute} ${hour} * * *"
|
||||
val clockTime = String.format("%02d:%02d", hour, minute)
|
||||
|
||||
if (scheduleToUpdate != null) {
|
||||
// Update existing schedule with new nextRunAt
|
||||
// Use the existing schedule's ID (not stableScheduleId) to ensure we update the right one
|
||||
db.scheduleDao().updateRunTimes(scheduleToUpdate.id, scheduleToUpdate.lastRunAt, triggerAtMillis)
|
||||
Log.d(SCHEDULE_TAG, "Updated schedule in database: id=${scheduleToUpdate.id}, nextRunAt=$triggerAtMillis (rollover)")
|
||||
} else {
|
||||
// No existing schedule found - create new one (shouldn't happen in normal flow)
|
||||
val newSchedule = Schedule(
|
||||
id = stableScheduleId,
|
||||
kind = "notify",
|
||||
cron = cronExpression,
|
||||
clockTime = clockTime,
|
||||
enabled = true,
|
||||
lastRunAt = null,
|
||||
nextRunAt = triggerAtMillis,
|
||||
jitterMs = 0,
|
||||
backoffPolicy = "exp",
|
||||
stateJson = null
|
||||
)
|
||||
db.scheduleDao().upsert(newSchedule)
|
||||
Log.d(SCHEDULE_TAG, "Created new schedule in database: id=$stableScheduleId, nextRunAt=$triggerAtMillis")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Log but don't fail - alarm is already scheduled, DB update is best-effort
|
||||
Log.w(SCHEDULE_TAG, "Failed to update schedule in database: $stableScheduleId (alarm still scheduled)", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -409,6 +479,7 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
// FIX: Use DailyNotificationReceiver to match what was scheduled
|
||||
val intent = Intent(context, com.timesafari.dailynotification.DailyNotificationReceiver::class.java).apply {
|
||||
setPackage(context.packageName)
|
||||
action = "com.timesafari.daily.NOTIFICATION"
|
||||
}
|
||||
val requestCode = when {
|
||||
@@ -419,14 +490,38 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
return
|
||||
}
|
||||
}
|
||||
val pendingIntent = PendingIntent.getBroadcast(
|
||||
|
||||
// CRITICAL: Use FLAG_NO_CREATE to get existing PendingIntent, don't create new one
|
||||
// This matches the pattern used in scheduleExactNotification for proper cancellation
|
||||
val existingPendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
alarmManager.cancel(pendingIntent)
|
||||
Log.i(TAG, "Notification alarm cancelled: scheduleId=$scheduleId, triggerAt=$triggerAtMillis, requestCode=$requestCode")
|
||||
|
||||
if (existingPendingIntent != null) {
|
||||
// Cancel both the alarm in AlarmManager AND the PendingIntent itself
|
||||
// This matches the pattern in scheduleExactNotification (lines 311-312)
|
||||
alarmManager.cancel(existingPendingIntent)
|
||||
existingPendingIntent.cancel()
|
||||
Log.i(TAG, "DNP-CANCEL: Notification alarm cancelled: scheduleId=$scheduleId, triggerAt=$triggerAtMillis, requestCode=$requestCode")
|
||||
|
||||
// Verify cancellation by checking if alarm still exists
|
||||
val verifyIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
if (verifyIntent == null) {
|
||||
Log.d(TAG, "DNP-CANCEL: ✅ Cancellation verified - no PendingIntent found for requestCode=$requestCode")
|
||||
} else {
|
||||
Log.w(TAG, "DNP-CANCEL: ⚠️ Cancellation may have failed - PendingIntent still exists for requestCode=$requestCode")
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "DNP-CANCEL: No existing PendingIntent found to cancel: scheduleId=$scheduleId, requestCode=$requestCode")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -440,6 +535,7 @@ class NotifyReceiver : BroadcastReceiver() {
|
||||
fun isAlarmScheduled(context: Context, scheduleId: String? = null, triggerAtMillis: Long? = null): Boolean {
|
||||
// FIX: Use DailyNotificationReceiver to match what was scheduled
|
||||
val intent = Intent(context, com.timesafari.dailynotification.DailyNotificationReceiver::class.java).apply {
|
||||
setPackage(context.packageName)
|
||||
action = "com.timesafari.daily.NOTIFICATION"
|
||||
}
|
||||
val requestCode = when {
|
||||
|
||||
@@ -41,6 +41,26 @@ class ReactivationManager(private val context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "DNP-REACTIVATION"
|
||||
private const val RECOVERY_TIMEOUT_SECONDS = 2L
|
||||
|
||||
/**
|
||||
* Load persisted title/body for a schedule from NotificationContentEntity (post-reboot recovery).
|
||||
* Tries schedule.id then "daily_${schedule.id}" to match NotifyReceiver/ScheduleHelper id convention.
|
||||
* Internal so BootReceiver can use when rescheduling after boot.
|
||||
*/
|
||||
internal fun getTitleBodyForSchedule(db: DailyNotificationDatabase, schedule: Schedule): Pair<String, String>? {
|
||||
val entity = try {
|
||||
db.notificationContentDao().getNotificationById(schedule.id)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
} ?: try {
|
||||
db.notificationContentDao().getNotificationById("daily_${schedule.id}")
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
} ?: return null
|
||||
val t = entity.title?.takeIf { it.isNotBlank() } ?: return null
|
||||
val b = entity.body?.takeIf { it.isNotBlank() } ?: return null
|
||||
return Pair(t, b)
|
||||
}
|
||||
|
||||
/**
|
||||
* Run boot-time recovery
|
||||
@@ -105,8 +125,8 @@ class ReactivationManager(private val context: Context) {
|
||||
markMissedNotificationForSchedule(schedule, nextRunTime, db)
|
||||
missedCount++
|
||||
|
||||
// Schedule next occurrence if repeating
|
||||
val nextOccurrence = calculateNextOccurrence(currentTime)
|
||||
// Schedule next occurrence (use rollover interval if set, else 24h)
|
||||
val nextOccurrence = calculateNextOccurrenceForSchedule(schedule, nextRunTime, currentTime)
|
||||
rescheduleAlarmForBoot(context, schedule, nextOccurrence, db)
|
||||
rescheduledCount++
|
||||
|
||||
@@ -218,10 +238,25 @@ class ReactivationManager(private val context: Context) {
|
||||
}
|
||||
|
||||
private fun calculateNextOccurrence(fromTime: Long): Long {
|
||||
// For daily schedules, add 24 hours
|
||||
// This is simplified - production should handle weekly/monthly patterns
|
||||
return fromTime + (24 * 60 * 60 * 1000L)
|
||||
}
|
||||
|
||||
/**
|
||||
* Next occurrence from a given trigger time. Uses schedule.rolloverIntervalMinutes when set and > 0 (dev/testing), else 24h.
|
||||
* Advances until result > currentTime so we don't reschedule in the past.
|
||||
*/
|
||||
private fun calculateNextOccurrenceForSchedule(schedule: Schedule, fromTime: Long, currentTime: Long): Long {
|
||||
val intervalMs = when {
|
||||
schedule.rolloverIntervalMinutes != null && schedule.rolloverIntervalMinutes!! > 0 ->
|
||||
schedule.rolloverIntervalMinutes!! * 60 * 1000L
|
||||
else -> 24 * 60 * 60 * 1000L
|
||||
}
|
||||
var next = fromTime + intervalMs
|
||||
while (next < currentTime) {
|
||||
next += intervalMs
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
private suspend fun markMissedNotificationForSchedule(
|
||||
schedule: Schedule,
|
||||
@@ -247,7 +282,7 @@ class ReactivationManager(private val context: Context) {
|
||||
// Create new notification content entry for missed alarm
|
||||
val notification = com.timesafari.dailynotification.entities.NotificationContentEntity(
|
||||
notificationId,
|
||||
"1.0.2", // Plugin version
|
||||
"1.3.0", // Plugin version
|
||||
null, // timesafariDid
|
||||
"daily", // notificationType
|
||||
"Daily Notification",
|
||||
@@ -275,22 +310,25 @@ class ReactivationManager(private val context: Context) {
|
||||
db: DailyNotificationDatabase
|
||||
) {
|
||||
try {
|
||||
val (title, body) = getTitleBodyForSchedule(db, schedule)
|
||||
?: Pair("Daily Notification", "Your daily update is ready")
|
||||
val config = UserNotificationConfig(
|
||||
enabled = schedule.enabled,
|
||||
schedule = schedule.cron ?: schedule.clockTime ?: "0 9 * * *",
|
||||
title = "Daily Notification",
|
||||
body = "Your daily update is ready",
|
||||
title = title,
|
||||
body = body,
|
||||
sound = true,
|
||||
vibration = true,
|
||||
priority = "normal"
|
||||
)
|
||||
|
||||
NotifyReceiver.scheduleExactNotification(
|
||||
context,
|
||||
nextRunTime,
|
||||
context,
|
||||
nextRunTime,
|
||||
config,
|
||||
scheduleId = schedule.id,
|
||||
source = ScheduleSource.BOOT_RECOVERY
|
||||
source = ScheduleSource.BOOT_RECOVERY,
|
||||
skipPendingIntentIdempotence = true
|
||||
)
|
||||
|
||||
// Update schedule in database (best effort)
|
||||
@@ -440,9 +478,9 @@ class ReactivationManager(private val context: Context) {
|
||||
*/
|
||||
private fun alarmsExist(): Boolean {
|
||||
return try {
|
||||
// Check if any PendingIntent for our receiver exists
|
||||
// This is more reliable than nextAlarmClock
|
||||
val intent = Intent(context, NotifyReceiver::class.java).apply {
|
||||
// Check if any PendingIntent for our receiver exists (must match NotifyReceiver schedule path)
|
||||
val intent = Intent(context, com.timesafari.dailynotification.DailyNotificationReceiver::class.java).apply {
|
||||
setPackage(context.packageName)
|
||||
action = "com.timesafari.daily.NOTIFICATION"
|
||||
}
|
||||
val pendingIntent = PendingIntent.getBroadcast(
|
||||
@@ -816,13 +854,13 @@ class ReactivationManager(private val context: Context) {
|
||||
db: DailyNotificationDatabase
|
||||
) {
|
||||
try {
|
||||
// Use existing BootReceiver logic for calculating next run time
|
||||
// For now, use schedule.nextRunAt directly
|
||||
val (title, body) = ReactivationManager.getTitleBodyForSchedule(db, schedule)
|
||||
?: Pair("Daily Notification", "Your daily update is ready")
|
||||
val config = UserNotificationConfig(
|
||||
enabled = schedule.enabled,
|
||||
schedule = schedule.cron ?: schedule.clockTime ?: "0 9 * * *",
|
||||
title = "Daily Notification",
|
||||
body = "Your daily update is ready",
|
||||
title = title,
|
||||
body = body,
|
||||
sound = true,
|
||||
vibration = true,
|
||||
priority = "normal"
|
||||
@@ -1014,7 +1052,7 @@ class ReactivationManager(private val context: Context) {
|
||||
// Create new notification content entry for missed alarm
|
||||
val notification = com.timesafari.dailynotification.entities.NotificationContentEntity(
|
||||
notificationId,
|
||||
"1.0.2", // Plugin version
|
||||
"1.3.0", // Plugin version
|
||||
null, // timesafariDid
|
||||
"daily", // notificationType
|
||||
"Daily Notification",
|
||||
@@ -1045,22 +1083,25 @@ class ReactivationManager(private val context: Context) {
|
||||
db: DailyNotificationDatabase
|
||||
) {
|
||||
try {
|
||||
val (title, body) = ReactivationManager.getTitleBodyForSchedule(db, schedule)
|
||||
?: Pair("Daily Notification", "Your daily update is ready")
|
||||
val config = UserNotificationConfig(
|
||||
enabled = schedule.enabled,
|
||||
schedule = schedule.cron ?: schedule.clockTime ?: "0 9 * * *",
|
||||
title = "Daily Notification",
|
||||
body = "Your daily update is ready",
|
||||
title = title,
|
||||
body = body,
|
||||
sound = true,
|
||||
vibration = true,
|
||||
priority = "normal"
|
||||
)
|
||||
|
||||
NotifyReceiver.scheduleExactNotification(
|
||||
context,
|
||||
nextRunTime,
|
||||
context,
|
||||
nextRunTime,
|
||||
config,
|
||||
scheduleId = schedule.id,
|
||||
source = ScheduleSource.BOOT_RECOVERY
|
||||
source = ScheduleSource.BOOT_RECOVERY,
|
||||
skipPendingIntentIdempotence = true
|
||||
)
|
||||
|
||||
// Update schedule in database (best effort)
|
||||
|
||||
@@ -52,7 +52,7 @@ public class DailyNotificationStorageRoom {
|
||||
private final ExecutorService executorService;
|
||||
|
||||
// Plugin version for migration tracking
|
||||
private static final String PLUGIN_VERSION = "1.0.0";
|
||||
private static final String PLUGIN_VERSION = "1.2.1";
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
||||
108
docs/ACTION_PLAN_INTEGRATION_FIXES.md
Normal file
108
docs/ACTION_PLAN_INTEGRATION_FIXES.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# Action Plan: Plugin + Consuming App Integration Fixes
|
||||
|
||||
**Source:** Comparison output from Cursor session (daily-notification-plugin ↔ Time Safari / crowd-funder-for-time-pwa).
|
||||
**Bugs addressed:** (A) Re-setting a notification doesn't fire; (B) Notification text always defaults to fallback values.
|
||||
|
||||
---
|
||||
|
||||
## Objective
|
||||
|
||||
Implement plugin-side and app-side changes so that:
|
||||
1. **Reset works:** Editing/re-saving a daily reminder (even with the same time) reliably re-schedules and the alarm fires.
|
||||
2. **Text persists:** Custom title/body persist across the first fire and rollover (next day); no silent fallback to generic text.
|
||||
3. **Cancel works on Android:** App can call `cancelDailyReminder({ reminderId })` and the plugin performs per-id cancellation (parity with iOS).
|
||||
|
||||
---
|
||||
|
||||
## Plugin-Side Implementation (this repo)
|
||||
|
||||
### 1. Bug A: Skip DB idempotence when caller requests reset
|
||||
|
||||
**File:** `android/src/main/java/com/timesafari/dailynotification/NotifyReceiver.kt`
|
||||
|
||||
**Problem:** `scheduleExactNotification()` already skips *PendingIntent* idempotence when `skipPendingIntentIdempotence=true`, but the **DB-level idempotence check** (lines ~206–226) still runs. On "re-set same time," the DB still has the same `nextRunAt`, so the check returns early and **no alarm is scheduled**.
|
||||
|
||||
**Change:** Wrap the entire DB idempotence block so it runs only when `!skipPendingIntentIdempotence`. When `skipPendingIntentIdempotence=true`, log and skip the DB check.
|
||||
|
||||
- **Locate:** The block starting with `// DB-LEVEL IDEMPOTENCE CHECK` that loads `existingSchedule` and compares `existingSchedule.nextRunAt` with `triggerAtMillis` (60s tolerance), and `return@runBlocking` on duplicate.
|
||||
- **Wrap:** Put that block inside `if (!skipPendingIntentIdempotence) { ... }` and add an `else` that logs:
|
||||
`"Skipping DB idempotence (skipPendingIntentIdempotence=true) for scheduleId=$stableScheduleId"`.
|
||||
|
||||
**Verification:** After editing a reminder without changing time, logs should show both "Skipping PendingIntent idempotence..." and "Skipping DB idempotence (skipPendingIntentIdempotence=true)...", and the alarm should fire.
|
||||
|
||||
---
|
||||
|
||||
### 2. Bug B: Preserve static reminder on rollover
|
||||
|
||||
**File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java`
|
||||
|
||||
**Problem:** In `scheduleNextNotification()`, the call to `NotifyReceiver.scheduleExactNotification()` uses **hardcoded** `false` for `isStaticReminder` and `null` for `reminderId`. So the *next* occurrence is treated as non-static and content is loaded from storage/default → fallback text.
|
||||
|
||||
**Change:**
|
||||
1. At the start of `scheduleNextNotification()`, read from WorkManager input:
|
||||
`boolean preserveStaticReminder = getInputData().getBoolean("is_static_reminder", false);`
|
||||
2. When choosing `scheduleId`: if `preserveStaticReminder && notificationId != null && !notificationId.isEmpty()`, set `scheduleId = notificationId`. Otherwise keep existing logic (`daily_*` → use as scheduleId, else `daily_rollover_` + timestamp).
|
||||
3. Replace the existing `scheduleExactNotification(...)` call with:
|
||||
- `isStaticReminder` = `preserveStaticReminder`
|
||||
- `reminderId` = `preserveStaticReminder ? scheduleId : null`
|
||||
- `scheduleId` = the chosen `scheduleId` (stable for static reminders).
|
||||
4. (Optional but useful) Add log before scheduling:
|
||||
`Log.d("DN|ROLLOVER", "next=" + nextScheduledTime + " scheduleId=" + scheduleId + " static=" + preserveStaticReminder);`
|
||||
|
||||
**Verification:** Set a custom title/body, let it fire once, then confirm the next scheduled run still uses the same text; logs should show `DN|ROLLOVER ... scheduleId=daily_timesafari_reminder static=true`.
|
||||
|
||||
---
|
||||
|
||||
### 3. Integration: Add Android `cancelDailyReminder`
|
||||
|
||||
**File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
|
||||
|
||||
**Problem:** The app calls `DailyNotification.cancelDailyReminder({ reminderId })`. iOS implements this; Android only has `cancelAllNotifications()` and `scheduleDailyReminder()` alias. On Android the call fails (method missing / not implemented), so "turn off" and "reset" flows cannot rely on explicit cancel.
|
||||
|
||||
**Change:** Add a new `@PluginMethod fun cancelDailyReminder(call: PluginCall)` (e.g. immediately after `scheduleDailyReminder()`).
|
||||
|
||||
- **Parse ID:** `reminderId = call.getString("reminderId") ?: call.getString("id") ?: call.getString("reminder_id") ?: call.getString("scheduleId")`. Reject if null/blank.
|
||||
- **Cancel alarm:** `NotifyReceiver.cancelNotification(context, scheduleId = reminderId)`.
|
||||
- **DB cleanup (best-effort):** In a try/catch, `runBlocking`:
|
||||
- `db = getDatabase()` (or `DailyNotificationDatabase.getDatabase(context)` as used elsewhere in plugin).
|
||||
- `db.scheduleDao().setEnabled(reminderId, false)` and `db.scheduleDao().updateRunTimes(reminderId, null, null)`.
|
||||
- ScheduleDao already has `setEnabled` and `updateRunTimes` (see `DatabaseSchema.kt`).
|
||||
- On success: `call.resolve()`. On exception: log and `call.reject("cancelDailyReminder failed: ...")`.
|
||||
|
||||
**Verification:** From the app, call `cancelDailyReminder({ reminderId: "daily_notification" })` (or your app’s id); it should resolve and the alarm for that id should be gone.
|
||||
|
||||
---
|
||||
|
||||
## Verification Checklist (plugin)
|
||||
|
||||
After implementing the three items above:
|
||||
|
||||
1. **Reset test:** Schedule reminder 2–3 minutes from now → Edit and re-save **without changing time** → Confirm it still fires. Logs: "Skipping DB idempotence (skipPendingIntentIdempotence=true)...".
|
||||
2. **Rollover test:** Set custom title/body → Let it fire once → Confirm next scheduled notification keeps the same title/body. Logs: `DN|ROLLOVER ... static=true scheduleId=daily_timesafari_reminder`.
|
||||
3. **Cancel test:** Call `cancelDailyReminder({ reminderId })` from app or test harness; no error and alarm cleared.
|
||||
|
||||
---
|
||||
|
||||
## Consuming App Work
|
||||
|
||||
App-side changes are described in a separate document intended for the **crowd-funder-for-time-pwa** (Time Safari) repo: **CONSUMING_APP_CURSOR_BRIEF.md**. That document is written so you can paste it into Cursor in the app repo to implement:
|
||||
|
||||
- Gate cancel in `editReminderNotification()` so Android skips pre-cancel (schedule path already cancels internally).
|
||||
- Replace `TimeSafariNativeFetcher` placeholder with real content fetch and token persistence if using native fetcher for daily content.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- NotifyReceiver: DB idempotence at ~206–226; skipPendingIntentIdempotence at ~159–204.
|
||||
- DailyNotificationWorker: `scheduleNextNotification()` ~512–594; pass `preserveStaticReminder` and stable `scheduleId` into `scheduleExactNotification`.
|
||||
- DailyNotificationPlugin: add `cancelDailyReminder` after `scheduleDailyReminder`; use `NotifyReceiver.cancelNotification` and ScheduleDao `setEnabled` / `updateRunTimes`.
|
||||
- DatabaseSchema.kt: ScheduleDao `getById`, `upsert`, `setEnabled`, `updateRunTimes`.
|
||||
|
||||
---
|
||||
|
||||
## Assumptions & Limits
|
||||
|
||||
- App uses a stable reminder id (e.g. `daily_timesafari_reminder`); plugin preserves that id for static reminders on rollover.
|
||||
- DAO method names are as in DatabaseSchema.kt; if the plugin’s Schedule entity uses different field names, adjust the `updateRunTimes` call accordingly (signature is `id, lastRunAt, nextRunAt`).
|
||||
- Native fetcher and token persistence are app responsibilities; the plugin only needs to preserve static reminder semantics and provide cancel-by-id.
|
||||
37
docs/CONSUMING_APP_ANDROID_NOTES.md
Normal file
37
docs/CONSUMING_APP_ANDROID_NOTES.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Consuming App Notes — Android Daily Notifications
|
||||
|
||||
Brief notes for apps that integrate the daily notification plugin on Android.
|
||||
|
||||
---
|
||||
|
||||
## Double schedule (rapid successive calls)
|
||||
|
||||
If your app calls `scheduleDailyNotification` twice in quick succession (e.g. within a few hundred ms) for the same reminder, the second call cancels the alarm just set and reschedules. On some devices or OEMs this can contribute to the alarm not firing.
|
||||
|
||||
**Recommendation:** Debounce or guard in the edit-reminder success path so you only call `scheduleDailyNotification` once per user action (e.g. wait for the first call to resolve before allowing another, or coalesce rapid calls).
|
||||
|
||||
---
|
||||
|
||||
## Alarm scheduled but not firing (e.g. 6:04)
|
||||
|
||||
When logs show "Scheduling OS alarm" and "Updated schedule in database" but the notification never appears:
|
||||
|
||||
1. **Confirm the broadcast is delivered**
|
||||
Run logcat including the receiver:
|
||||
```bash
|
||||
adb logcat -v time -s DNP-SCHEDULE:V DailyNotificationWorker:V DailyNotificationReceiver:V
|
||||
```
|
||||
At the scheduled time, check whether `DailyNotificationReceiver` logs anything. If the Receiver runs, the issue is downstream (WorkManager / display). If it does not run, the OS did not deliver the alarm (Doze, OEM, or alarm replacement).
|
||||
|
||||
2. **Avoid double schedule**
|
||||
Ensure the app is not calling `scheduleDailyNotification` twice in quick succession for the same reminder (see above).
|
||||
|
||||
3. **Plugin fix (v1.1.6+)**
|
||||
The plugin no longer overwrites the app’s schedule row when handling rollover work that uses a `daily_rollover_*` id, so the app’s `nextRunAt` stays correct after a notification fires.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- [ACTION_PLAN_INTEGRATION_FIXES.md](./ACTION_PLAN_INTEGRATION_FIXES.md) — plugin and app integration checklist
|
||||
- [CONSUMING_APP_OPTIONAL_ANDROID_ID_CLEANUP.md](./CONSUMING_APP_OPTIONAL_ANDROID_ID_CLEANUP.md) — optional cleanup of stale schedule rows
|
||||
136
docs/CONSUMING_APP_OPTIONAL_ANDROID_ID_CLEANUP.md
Normal file
136
docs/CONSUMING_APP_OPTIONAL_ANDROID_ID_CLEANUP.md
Normal file
@@ -0,0 +1,136 @@
|
||||
# Optional: Use a Single Stable Schedule ID on iOS and Android
|
||||
|
||||
**Audience:** Consuming apps (e.g. TimeSafari / crowd-funder-for-time-pwa) that use `@timesafari/daily-notification-plugin`.
|
||||
**Purpose:** Describe an optional app-side cleanup now that the plugin’s Android second-schedule bug is fixed (plugin v1.1.2+).
|
||||
**Use:** Feed this doc into Cursor (or any editor) in the consuming app repo when implementing the cleanup.
|
||||
|
||||
---
|
||||
|
||||
## Context
|
||||
|
||||
- **Plugin fix (v1.1.2):** After cancel-then-schedule on Android, the plugin no longer skips the new schedule due to PendingIntent cache. Rescheduling works reliably whether or not the app passes an explicit `id` to `scheduleDailyNotification`.
|
||||
- **Previous workaround:** Some apps avoided passing `id` on Android and used the plugin default `"daily_notification"` so that the (now-fixed) second-schedule bug would not trigger. On iOS they passed a stable id (e.g. `"daily_timesafari_reminder"`) for getStatus/cancel and verification.
|
||||
- **Optional cleanup:** You can use the **same** stable schedule id on both iOS and Android. That simplifies code (one id everywhere), makes getStatus/cancel and verification consistent across platforms, and is safe with plugin v1.1.2+.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Depend on **`@timesafari/daily-notification-plugin@1.1.2`** (or `^1.1.2`) so the Android fix is in effect.
|
||||
- No other code changes are required for the bug fix; this doc is only for the optional id cleanup.
|
||||
|
||||
---
|
||||
|
||||
## What to Change in the Consuming App
|
||||
|
||||
### 1. Single stable reminder ID (both platforms)
|
||||
|
||||
Use one reminder id for schedule, cancel, and getStatus on both iOS and Android.
|
||||
|
||||
**Example (current pattern):**
|
||||
|
||||
```ts
|
||||
// Before: different id per platform
|
||||
private get reminderId(): string {
|
||||
return Capacitor.getPlatform() === "ios"
|
||||
? "daily_timesafari_reminder"
|
||||
: "daily_notification";
|
||||
}
|
||||
```
|
||||
|
||||
**After (optional cleanup):**
|
||||
|
||||
```ts
|
||||
// After: same stable id on both platforms (requires plugin >= 1.1.2)
|
||||
private readonly reminderId = "daily_timesafari_reminder";
|
||||
```
|
||||
|
||||
Or keep a getter if you prefer:
|
||||
|
||||
```ts
|
||||
private get reminderId(): string {
|
||||
return "daily_timesafari_reminder";
|
||||
}
|
||||
```
|
||||
|
||||
Use whatever stable string your app already uses on iOS (e.g. `"daily_timesafari_reminder"`); no need to change the value.
|
||||
|
||||
---
|
||||
|
||||
### 2. Pass `id` when scheduling on Android
|
||||
|
||||
Today you may only add `scheduleOptions.id` on iOS. Add it for Android too so the plugin stores and returns this id (getStatus, getScheduledReminders, cancel all use it).
|
||||
|
||||
**Example (current pattern):**
|
||||
|
||||
```ts
|
||||
const scheduleOptions = {
|
||||
time: options.time,
|
||||
title: options.title,
|
||||
body: options.body,
|
||||
sound: true,
|
||||
priority: (options.priority || "normal") as "low" | "default" | "high",
|
||||
};
|
||||
if (Capacitor.getPlatform() === "ios") {
|
||||
scheduleOptions.id = this.reminderId;
|
||||
}
|
||||
await DailyNotification.scheduleDailyNotification(scheduleOptions);
|
||||
```
|
||||
|
||||
**After (optional cleanup):**
|
||||
|
||||
```ts
|
||||
const scheduleOptions = {
|
||||
time: options.time,
|
||||
title: options.title,
|
||||
body: options.body,
|
||||
sound: true,
|
||||
priority: (options.priority || "normal") as "low" | "default" | "high",
|
||||
id: this.reminderId, // same id on iOS and Android (plugin >= 1.1.2)
|
||||
};
|
||||
await DailyNotification.scheduleDailyNotification(scheduleOptions);
|
||||
```
|
||||
|
||||
So: always pass `id: this.reminderId` (or your chosen constant) for both platforms.
|
||||
|
||||
---
|
||||
|
||||
### 3. Update comments
|
||||
|
||||
Remove or update comments that say Android must not receive an `id` to avoid the second-schedule bug, and that the plugin uses `"daily_notification"` on Android. Replace with a short note that a single stable id is used on both platforms and requires plugin v1.1.2+.
|
||||
|
||||
**Example comment to add/update:**
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Stable schedule/reminder ID used for schedule, cancel, and getStatus.
|
||||
* Same value on iOS and Android (plugin v1.1.2+ fixes Android reschedule with custom id).
|
||||
*/
|
||||
private readonly reminderId = "daily_timesafari_reminder";
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Files to Touch (typical)
|
||||
|
||||
- **Native notification service** (e.g. `src/services/notifications/NativeNotificationService.ts`):
|
||||
- `reminderId`: use single value for both platforms.
|
||||
- `scheduleDailyNotification`: always pass `id` in `scheduleOptions` (include Android).
|
||||
- Adjust comments as above.
|
||||
|
||||
No changes are required to cancel or getStatus if they already use `this.reminderId`; they will now resolve the same schedule on Android as on iOS.
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
1. **Android:** Schedule a daily notification, then change time and save again (reschedule). The second scheduled time should fire; no need to reinstall.
|
||||
2. **getStatus:** After scheduling on Android, getStatus should return the scheduled reminder with the same id you pass (e.g. `daily_timesafari_reminder`).
|
||||
3. **Cancel:** Cancelling by that id on Android should clear the scheduled notification.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- Plugin CHANGELOG: `[1.1.2] - 2026-02-13` — Android second daily notification not firing after reschedule.
|
||||
- Issue context (if present in consuming app): `doc/android-daily-notification-second-schedule-issue.md`.
|
||||
462
docs/TIMESAFARI_ANDROID_COMPARISON.md
Normal file
462
docs/TIMESAFARI_ANDROID_COMPARISON.md
Normal file
@@ -0,0 +1,462 @@
|
||||
# Android Notification Implementation Comparison
|
||||
|
||||
**Test App (Working)** vs **TimeSafari (Not Working)**
|
||||
|
||||
This document identifies the critical differences between the test app where notifications work correctly and the TimeSafari app where notifications don't work at all. Use this as a checklist to fix TimeSafari.
|
||||
|
||||
---
|
||||
|
||||
## Critical Issues (Must Fix)
|
||||
|
||||
### 1. Missing Custom Application Class
|
||||
|
||||
**This is likely the primary cause of failure.**
|
||||
|
||||
**Test App (Working):**
|
||||
```xml
|
||||
<!-- AndroidManifest.xml -->
|
||||
<application
|
||||
android:name=".TestApplication"
|
||||
...>
|
||||
```
|
||||
|
||||
```java
|
||||
// TestApplication.java
|
||||
public class TestApplication extends Application {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
Context context = getApplicationContext();
|
||||
NativeNotificationContentFetcher testFetcher =
|
||||
new com.timesafari.dailynotification.test.TestNativeFetcher(context);
|
||||
DailyNotificationPlugin.setNativeFetcher(testFetcher);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
```xml
|
||||
<!-- AndroidManifest.xml - NO android:name attribute -->
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
...>
|
||||
```
|
||||
- No custom Application class exists
|
||||
- No native fetcher is registered
|
||||
- Plugin cannot fetch notification content
|
||||
|
||||
**Fix Required:**
|
||||
1. Create `TimeSafariApplication.java` in `android/app/src/main/java/app/timesafari/`
|
||||
2. Implement `NativeNotificationContentFetcher` specific to TimeSafari
|
||||
3. Add `android:name=".TimeSafariApplication"` to AndroidManifest.xml
|
||||
|
||||
---
|
||||
|
||||
### 2. Missing Capacitor Plugin Configuration
|
||||
|
||||
**Test App (Working):**
|
||||
```typescript
|
||||
// capacitor.config.ts
|
||||
plugins: {
|
||||
DailyNotification: {
|
||||
debugMode: true,
|
||||
enableNotifications: true,
|
||||
timesafariConfig: {
|
||||
activeDid: "did:ethr:0x...",
|
||||
endpoints: {
|
||||
projectsLastUpdated: "http://..."
|
||||
},
|
||||
starredProjectsConfig: {
|
||||
enabled: true,
|
||||
starredPlanHandleIds: [...],
|
||||
fetchInterval: '0 8 * * *'
|
||||
},
|
||||
credentialConfig: {
|
||||
jwtSecret: '...',
|
||||
tokenExpirationMinutes: 1
|
||||
}
|
||||
},
|
||||
networkConfig: {
|
||||
timeout: 30000,
|
||||
retryAttempts: 3,
|
||||
retryDelay: 1000
|
||||
},
|
||||
contentFetch: {
|
||||
enabled: true,
|
||||
schedule: '0 00 * * *',
|
||||
fetchLeadTimeMinutes: 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
```typescript
|
||||
// capacitor.config.ts - NO DailyNotification configuration at all
|
||||
plugins: {
|
||||
App: { ... },
|
||||
SplashScreen: { ... },
|
||||
CapSQLite: { ... }
|
||||
// DailyNotification is MISSING
|
||||
}
|
||||
```
|
||||
|
||||
**Fix Required:**
|
||||
Add `DailyNotification` configuration to `capacitor.config.ts` with appropriate values for TimeSafari.
|
||||
|
||||
---
|
||||
|
||||
### 3. Missing Permissions in AndroidManifest.xml
|
||||
|
||||
**Test App has these permissions that TimeSafari is missing:**
|
||||
|
||||
```xml
|
||||
<!-- Add to TimeSafari's AndroidManifest.xml -->
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
```
|
||||
|
||||
**Current TimeSafari permissions (incomplete):**
|
||||
- ✅ `INTERNET`
|
||||
- ✅ `POST_NOTIFICATIONS`
|
||||
- ✅ `SCHEDULE_EXACT_ALARM`
|
||||
- ✅ `USE_EXACT_ALARM`
|
||||
- ✅ `RECEIVE_BOOT_COMPLETED`
|
||||
- ✅ `WAKE_LOCK`
|
||||
- ❌ `ACCESS_NETWORK_STATE` - **MISSING**
|
||||
- ❌ `FOREGROUND_SERVICE` - **MISSING**
|
||||
- ❌ `SYSTEM_ALERT_WINDOW` - **MISSING**
|
||||
- ❌ `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` - **MISSING**
|
||||
|
||||
---
|
||||
|
||||
### 4. Missing Gradle Dependencies
|
||||
|
||||
**Test App (Working):**
|
||||
```gradle
|
||||
// android/app/build.gradle
|
||||
dependencies {
|
||||
// Capacitor annotation processor for automatic plugin discovery
|
||||
annotationProcessor project(':capacitor-android')
|
||||
|
||||
// Required dependencies for the plugin
|
||||
implementation 'androidx.work:work-runtime:2.9.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.7.0'
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
}
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
```gradle
|
||||
dependencies {
|
||||
// Missing: annotationProcessor project(':capacitor-android')
|
||||
implementation "androidx.work:work-runtime-ktx:2.9.0" // Using Kotlin version
|
||||
// Missing: androidx.lifecycle:lifecycle-service
|
||||
// Missing: com.google.code.gson:gson
|
||||
}
|
||||
```
|
||||
|
||||
**Fix Required:**
|
||||
Add to TimeSafari's `android/app/build.gradle`:
|
||||
```gradle
|
||||
annotationProcessor project(':capacitor-android')
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.7.0'
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Secondary Issues (Should Fix)
|
||||
|
||||
### 5. DailyNotificationReceiver Export Status
|
||||
|
||||
**Test App (Working):**
|
||||
```xml
|
||||
<receiver
|
||||
android:name="com.timesafari.dailynotification.DailyNotificationReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false"> <!-- Note: false -->
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
```xml
|
||||
<receiver
|
||||
android:name="com.timesafari.dailynotification.DailyNotificationReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true"> <!-- Note: true - potential security issue -->
|
||||
```
|
||||
|
||||
The test app uses `exported="false"` because the plugin creates PendingIntents with explicit component targeting. Using `exported="true"` is unnecessary and a potential security concern.
|
||||
|
||||
---
|
||||
|
||||
### 6. Missing Network Security Config
|
||||
|
||||
**Test App (Working):**
|
||||
```xml
|
||||
<application
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
...>
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
```xml
|
||||
<application>
|
||||
<!-- No networkSecurityConfig -->
|
||||
```
|
||||
|
||||
This may affect HTTP (non-HTTPS) requests during development.
|
||||
|
||||
---
|
||||
|
||||
### 7. Missing Java Compile Options
|
||||
|
||||
**Test App (Working):**
|
||||
```gradle
|
||||
android {
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**TimeSafari (Broken):**
|
||||
No explicit compile options set.
|
||||
|
||||
---
|
||||
|
||||
## Complete Fix Checklist
|
||||
|
||||
### Step 1: Create Custom Application Class
|
||||
|
||||
Create file: `android/app/src/main/java/app/timesafari/TimeSafariApplication.java`
|
||||
|
||||
```java
|
||||
package app.timesafari;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import com.timesafari.dailynotification.DailyNotificationPlugin;
|
||||
import com.timesafari.dailynotification.NativeNotificationContentFetcher;
|
||||
|
||||
public class TimeSafariApplication extends Application {
|
||||
|
||||
private static final String TAG = "TimeSafariApplication";
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
Log.i(TAG, "Initializing TimeSafari notifications");
|
||||
|
||||
// Register native fetcher with application context
|
||||
Context context = getApplicationContext();
|
||||
NativeNotificationContentFetcher fetcher =
|
||||
new TimeSafariNativeFetcher(context);
|
||||
DailyNotificationPlugin.setNativeFetcher(fetcher);
|
||||
|
||||
Log.i(TAG, "Native fetcher registered");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2: Create Native Fetcher Implementation
|
||||
|
||||
Create file: `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java`
|
||||
|
||||
```java
|
||||
package app.timesafari;
|
||||
|
||||
import android.content.Context;
|
||||
import com.timesafari.dailynotification.NativeNotificationContentFetcher;
|
||||
import com.timesafari.dailynotification.NotificationContent;
|
||||
|
||||
public class TimeSafariNativeFetcher implements NativeNotificationContentFetcher {
|
||||
|
||||
private final Context context;
|
||||
|
||||
public TimeSafariNativeFetcher(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationContent fetchContent(String scheduleId) {
|
||||
// TODO: Implement actual content fetching for TimeSafari
|
||||
// This should query the TimeSafari API for notification content
|
||||
return new NotificationContent(
|
||||
"timesafari_" + System.currentTimeMillis(),
|
||||
"TimeSafari Update",
|
||||
"Check your starred projects for updates!",
|
||||
System.currentTimeMillis(),
|
||||
null,
|
||||
System.currentTimeMillis()
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3: Update AndroidManifest.xml
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application
|
||||
android:name=".TimeSafariApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
<!-- ... existing content ... -->
|
||||
|
||||
<!-- Fix: Change exported to false -->
|
||||
<receiver
|
||||
android:name="com.timesafari.dailynotification.DailyNotificationReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.timesafari.daily.NOTIFICATION" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- ... rest of receivers ... -->
|
||||
|
||||
</application>
|
||||
|
||||
<!-- Existing permissions -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<!-- ADD these missing permissions -->
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
</manifest>
|
||||
```
|
||||
|
||||
### Step 4: Update build.gradle
|
||||
|
||||
Add to `android/app/build.gradle`:
|
||||
|
||||
```gradle
|
||||
android {
|
||||
// ... existing config ...
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// ... existing dependencies ...
|
||||
|
||||
// ADD these for notification plugin
|
||||
annotationProcessor project(':capacitor-android')
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.7.0'
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
}
|
||||
```
|
||||
|
||||
### Step 5: Update capacitor.config.ts
|
||||
|
||||
Add DailyNotification configuration:
|
||||
|
||||
```typescript
|
||||
plugins: {
|
||||
// ... existing plugins ...
|
||||
|
||||
DailyNotification: {
|
||||
debugMode: true,
|
||||
enableNotifications: true,
|
||||
timesafariConfig: {
|
||||
activeDid: '', // Will be set dynamically from user's DID
|
||||
endpoints: {
|
||||
projectsLastUpdated: 'https://api.endorser.ch/api/v2/report/plansLastUpdatedBetween'
|
||||
},
|
||||
starredProjectsConfig: {
|
||||
enabled: true,
|
||||
starredPlanHandleIds: [],
|
||||
fetchInterval: '0 8 * * *'
|
||||
}
|
||||
},
|
||||
networkConfig: {
|
||||
timeout: 30000,
|
||||
retryAttempts: 3,
|
||||
retryDelay: 1000
|
||||
},
|
||||
contentFetch: {
|
||||
enabled: true,
|
||||
schedule: '0 8 * * *',
|
||||
fetchLeadTimeMinutes: 5
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 6: Rebuild
|
||||
|
||||
```bash
|
||||
npx cap sync android
|
||||
cd android && ./gradlew clean
|
||||
cd .. && npx cap build android
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
After implementing fixes, verify:
|
||||
|
||||
1. **Check logs for Application initialization:**
|
||||
```bash
|
||||
adb logcat | grep -E "TimeSafariApplication|Native fetcher"
|
||||
```
|
||||
|
||||
2. **Check alarm scheduling:**
|
||||
```bash
|
||||
adb shell dumpsys alarm | grep -i timesafari
|
||||
```
|
||||
|
||||
3. **Test receiver manually:**
|
||||
```bash
|
||||
adb shell am broadcast -a com.timesafari.daily.NOTIFICATION \
|
||||
--es id "test_notification" \
|
||||
-n app.timesafari.app/com.timesafari.dailynotification.DailyNotificationReceiver
|
||||
```
|
||||
|
||||
4. **Check notification permissions:**
|
||||
```bash
|
||||
adb shell dumpsys package app.timesafari.app | grep -A 5 "granted=true"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Summary of Critical Differences
|
||||
|
||||
| Component | Test App (Working) | TimeSafari (Broken) |
|
||||
|-----------|-------------------|---------------------|
|
||||
| Custom Application class | ✅ TestApplication.java | ❌ None |
|
||||
| Native fetcher registration | ✅ In Application.onCreate() | ❌ Not registered |
|
||||
| DailyNotification config | ✅ Full config in capacitor.config.ts | ❌ Not configured |
|
||||
| ACCESS_NETWORK_STATE | ✅ Present | ❌ Missing |
|
||||
| FOREGROUND_SERVICE | ✅ Present | ❌ Missing |
|
||||
| REQUEST_IGNORE_BATTERY_OPTIMIZATIONS | ✅ Present | ❌ Missing |
|
||||
| Gson dependency | ✅ Present | ❌ Missing |
|
||||
| lifecycle-service dependency | ✅ Present | ❌ Missing |
|
||||
| Capacitor annotation processor | ✅ Present | ❌ Missing |
|
||||
|
||||
**The most critical missing piece is the custom Application class with native fetcher registration.** Without this, the plugin has no way to fetch notification content when the alarm fires.
|
||||
@@ -1,8 +1,8 @@
|
||||
# Running Android App in Standalone Emulator (Without Android Studio)
|
||||
|
||||
**Author**: Matthew Raymer
|
||||
**Last Updated**: 2025-10-12 06:50:00 UTC
|
||||
**Version**: 1.0.0
|
||||
**Last Updated**: 2026-02-05
|
||||
**Version**: 1.1.0
|
||||
|
||||
## Overview
|
||||
|
||||
@@ -22,6 +22,81 @@ This guide demonstrates how to run the DailyNotification plugin test app in a st
|
||||
- **Storage**: 2GB free space for emulator
|
||||
- **OS**: Linux, macOS, or Windows with WSL
|
||||
|
||||
## Checking and Installing Prerequisites
|
||||
|
||||
### How to check
|
||||
|
||||
Run these in a terminal. If a command is missing or a check fails, use the install steps below.
|
||||
|
||||
| Requirement | How to check |
|
||||
|------------------|--------------|
|
||||
| **Node.js** | `node --version` (v14+ recommended; test app may require 20+) |
|
||||
| **npm** | `npm --version` |
|
||||
| **Java** | `java -version` (Java 11+; build scripts expect 11+) |
|
||||
| **ANDROID_HOME** | `echo $ANDROID_HOME` (must be set to your Android SDK root) |
|
||||
| **adb** | `adb version` (must be on PATH; usually `$ANDROID_HOME/platform-tools/adb`) |
|
||||
| **emulator** | `emulator -version` (must be on PATH; usually `$ANDROID_HOME/emulator/emulator`) |
|
||||
| **At least one AVD** | `emulator -list-avds` (must list at least one device name) |
|
||||
|
||||
**Project script:** From the repo root you can run:
|
||||
|
||||
```bash
|
||||
node scripts/check-environment.js
|
||||
```
|
||||
|
||||
This checks Node, npm, Java, and `ANDROID_HOME`. It does **not** check `adb`, `emulator`, or AVDs—verify those manually as above.
|
||||
|
||||
### How to install
|
||||
|
||||
- **Node.js and npm**
|
||||
- Install from [nodejs.org](https://nodejs.org/) (LTS), or on macOS: `brew install node`.
|
||||
|
||||
- **Java (JDK 11+)**
|
||||
- macOS: `brew install openjdk@17` and follow the caveats to link (e.g. `sudo ln -sfn $(brew --prefix)/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk`).
|
||||
- Or install [Eclipse Temurin](https://adoptium.net/) / [Oracle JDK](https://www.oracle.com/java/technologies/downloads/) and ensure `java` and `javac` are on your PATH.
|
||||
|
||||
- **Android SDK (without Android Studio)**
|
||||
1. Download the [Command-line tools only](https://developer.android.com/studio#command-tools) package for your OS.
|
||||
2. Create an SDK directory, e.g. `mkdir -p ~/android-sdk` and extract the zip so that you have `~/android-sdk/cmdline-tools/latest/` (the `bin` folder with `sdkmanager` and `avdmanager` must be inside `cmdline-tools/latest/`).
|
||||
3. Set environment variables (add to `~/.zshrc` or `~/.bashrc`):
|
||||
|
||||
```bash
|
||||
export ANDROID_HOME=$HOME/android-sdk
|
||||
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator
|
||||
```
|
||||
|
||||
4. Install required SDK packages (accept licenses when prompted):
|
||||
|
||||
```bash
|
||||
sdkmanager "platform-tools"
|
||||
sdkmanager "emulator"
|
||||
sdkmanager "platforms;android-35"
|
||||
sdkmanager "build-tools;35.0.0"
|
||||
```
|
||||
|
||||
Install a system image that matches your host CPU:
|
||||
- **Apple Silicon (M1/M2/M3, aarch64):** `sdkmanager "system-images;android-35;google_apis;arm64-v8a"`
|
||||
- **Intel Mac / Windows / Linux (x86_64):** `sdkmanager "system-images;android-35;google_apis;x86_64"`
|
||||
|
||||
5. Create at least one AVD (use the same image type you installed):
|
||||
|
||||
**Apple Silicon:**
|
||||
```bash
|
||||
avdmanager create avd -n Pixel8_API35 -k "system-images;android-35;google_apis;arm64-v8a" -d "pixel_8"
|
||||
```
|
||||
|
||||
**Intel / x86_64:**
|
||||
```bash
|
||||
avdmanager create avd -n Pixel8_API35 -k "system-images;android-35;google_apis;x86_64" -d "pixel_8"
|
||||
```
|
||||
|
||||
Then start the emulator with: `emulator -avd Pixel8_API35 -no-snapshot-load &` and use `adb wait-for-device` before building/installing the app.
|
||||
|
||||
- **Gradle**
|
||||
The project uses the Gradle Wrapper (`gradlew`) inside the app’s `android` directory. No separate Gradle install is needed.
|
||||
|
||||
After installing, run the checks again to confirm `adb`, `emulator`, and `emulator -list-avds` work.
|
||||
|
||||
## Step-by-Step Process
|
||||
|
||||
### 1. Check Available Emulators
|
||||
@@ -31,21 +106,21 @@ This guide demonstrates how to run the DailyNotification plugin test app in a st
|
||||
emulator -list-avds
|
||||
|
||||
# Example output:
|
||||
# Pixel8_API34
|
||||
# Pixel8_API35
|
||||
```
|
||||
|
||||
### 2. Start the Emulator
|
||||
|
||||
```bash
|
||||
# Start emulator in background (recommended)
|
||||
emulator -avd Pixel8_API34 -no-snapshot-load &
|
||||
emulator -avd Pixel8_API35 -no-snapshot-load &
|
||||
|
||||
# Alternative: Start in foreground
|
||||
emulator -avd Pixel8_API34
|
||||
emulator -avd Pixel8_API35
|
||||
```
|
||||
|
||||
**Flags Explained:**
|
||||
- `-avd Pixel8_API34` - Specifies the AVD to use
|
||||
- `-avd Pixel8_API35` - Specifies the AVD to use
|
||||
- `-no-snapshot-load` - Forces fresh boot (recommended for testing)
|
||||
- `&` - Runs in background (optional)
|
||||
|
||||
@@ -141,7 +216,7 @@ adb logcat -c && adb logcat
|
||||
|
||||
```bash
|
||||
# 1. Start emulator
|
||||
emulator -avd Pixel8_API34 -no-snapshot-load &
|
||||
emulator -avd Pixel8_API35 -no-snapshot-load &
|
||||
|
||||
# 2. Wait for emulator
|
||||
adb wait-for-device
|
||||
@@ -211,7 +286,17 @@ ps aux | grep emulator
|
||||
pkill -f emulator
|
||||
|
||||
# Start with verbose logging
|
||||
emulator -avd Pixel8_API34 -verbose
|
||||
emulator -avd Pixel8_API35 -verbose
|
||||
```
|
||||
|
||||
#### "x86_64 is not supported by the QEMU2 emulator on aarch64 host"
|
||||
On Apple Silicon (M1/M2/M3), the emulator cannot run x86_64 system images. Use an ARM64 image and AVD instead:
|
||||
|
||||
```bash
|
||||
sdkmanager "system-images;android-35;google_apis;arm64-v8a"
|
||||
avdmanager delete avd -n Pixel8_API35 # if you already created an x86_64 AVD
|
||||
avdmanager create avd -n Pixel8_API35 -k "system-images;android-35;google_apis;arm64-v8a" -d "pixel_8"
|
||||
emulator -avd Pixel8_API35 -no-snapshot-load
|
||||
```
|
||||
|
||||
#### ADB Connection Issues
|
||||
@@ -256,13 +341,13 @@ cd android && ./gradlew clean
|
||||
#### Emulator Performance
|
||||
```bash
|
||||
# Start with hardware acceleration
|
||||
emulator -avd Pixel8_API34 -accel on
|
||||
emulator -avd Pixel8_API35 -accel on
|
||||
|
||||
# Start with specific RAM allocation
|
||||
emulator -avd Pixel8_API34 -memory 2048
|
||||
emulator -avd Pixel8_API35 -memory 2048
|
||||
|
||||
# Start with GPU acceleration
|
||||
emulator -avd Pixel8_API34 -gpu host
|
||||
emulator -avd Pixel8_API35 -gpu host
|
||||
```
|
||||
|
||||
#### Build Performance
|
||||
@@ -336,7 +421,7 @@ adb shell am start -n com.timesafari.dailynotification/.MainActivity
|
||||
### Automated Testing
|
||||
```bash
|
||||
# CI/CD pipeline
|
||||
emulator -avd Pixel8_API34 -no-snapshot-load &
|
||||
emulator -avd Pixel8_API35 -no-snapshot-load &
|
||||
adb wait-for-device
|
||||
./scripts/build-native.sh --platform android
|
||||
cd android && ./gradlew :app:assembleDebug
|
||||
|
||||
519
docs/testing/PHYSICAL_DEVICE_GUIDE.md
Normal file
519
docs/testing/PHYSICAL_DEVICE_GUIDE.md
Normal file
@@ -0,0 +1,519 @@
|
||||
# Running Android App on a Physical Device
|
||||
|
||||
**Author**: Matthew Raymer
|
||||
**Last Updated**: 2026-02-12
|
||||
**Version**: 1.0.0
|
||||
|
||||
## Overview
|
||||
|
||||
This guide demonstrates how to run the DailyNotification plugin test app on a physical Android device. Physical device testing is essential for validating:
|
||||
|
||||
- **Real notification behavior** — Emulators may not accurately simulate notification delivery timing
|
||||
- **Battery optimization effects** — OEM-specific power management that affects background tasks
|
||||
- **Actual alarm scheduling** — AlarmManager behavior varies between emulators and real hardware
|
||||
- **Device reboot persistence** — Boot receivers and alarm recovery
|
||||
|
||||
## Prerequisites
|
||||
|
||||
### Required Hardware
|
||||
- **Android phone or tablet** running Android 8.0 (API 26) or higher
|
||||
- **USB cable** (data-capable, not charge-only)
|
||||
- **Development computer** with USB port
|
||||
|
||||
### Required Software
|
||||
- **Android SDK** with platform-tools (provides `adb`)
|
||||
- **Gradle** (via Gradle Wrapper)
|
||||
- **Node.js** and **npm** (for TypeScript compilation)
|
||||
|
||||
### How to Check
|
||||
|
||||
| Requirement | How to check |
|
||||
|------------------|--------------|
|
||||
| **Node.js** | `node --version` (v14+ recommended; test app may require 20+) |
|
||||
| **npm** | `npm --version` |
|
||||
| **Java** | `java -version` (Java 11+) |
|
||||
| **ANDROID_HOME** | `echo $ANDROID_HOME` (must be set to your Android SDK root) |
|
||||
| **adb** | `adb version` (must be on PATH) |
|
||||
|
||||
**Project script:** From the repo root:
|
||||
|
||||
```bash
|
||||
node scripts/check-environment.js
|
||||
```
|
||||
|
||||
## Step 1: Enable Developer Options on Your Phone
|
||||
|
||||
Developer Options are hidden by default. To enable them:
|
||||
|
||||
### Android 8.0 - 14 (Most Devices)
|
||||
|
||||
1. Open **Settings**
|
||||
2. Scroll down to **About phone** (or **About device**)
|
||||
3. Find **Build number**
|
||||
4. **Tap Build number 7 times** rapidly
|
||||
5. You'll see "You are now a developer!" toast message
|
||||
|
||||
### Samsung Devices
|
||||
|
||||
1. **Settings** → **About phone** → **Software information**
|
||||
2. Tap **Build number** 7 times
|
||||
|
||||
### Xiaomi/MIUI Devices
|
||||
|
||||
1. **Settings** → **About phone**
|
||||
2. Tap **MIUI version** 7 times
|
||||
|
||||
### OnePlus Devices
|
||||
|
||||
1. **Settings** → **About phone**
|
||||
2. Tap **Build number** 7 times
|
||||
|
||||
## Step 2: Enable USB Debugging
|
||||
|
||||
After enabling Developer Options:
|
||||
|
||||
1. Go to **Settings** → **System** → **Developer options**
|
||||
- On some phones: **Settings** → **Developer options** directly
|
||||
2. Scroll to find **USB debugging**
|
||||
3. Toggle **USB debugging ON**
|
||||
4. Confirm when prompted
|
||||
|
||||
### Optional but Recommended Settings
|
||||
|
||||
While in Developer Options, also enable:
|
||||
|
||||
- **Stay awake** — Screen stays on while charging (useful during development)
|
||||
- **Allow mock locations** — If testing location features
|
||||
|
||||
## Step 3: Connect and Authorize Your Device
|
||||
|
||||
### Physical Connection
|
||||
|
||||
1. Connect your phone to your computer via USB
|
||||
2. On your phone, change USB mode:
|
||||
- Pull down notification shade
|
||||
- Tap the USB notification ("Charging this device via USB")
|
||||
- Select **File transfer / Android Auto** or **PTP** (not "Charge only")
|
||||
|
||||
### Authorize Computer
|
||||
|
||||
1. On your phone, you'll see a dialog: **"Allow USB debugging?"**
|
||||
2. Check **"Always allow from this computer"** (recommended)
|
||||
3. Tap **Allow**
|
||||
|
||||
### Verify Connection
|
||||
|
||||
```bash
|
||||
# List connected devices
|
||||
adb devices
|
||||
|
||||
# Expected output:
|
||||
# List of devices attached
|
||||
# ABC123DEF456 device
|
||||
```
|
||||
|
||||
**Troubleshooting connection states:**
|
||||
|
||||
| State | Meaning | Solution |
|
||||
|-------|---------|----------|
|
||||
| `device` | Connected and authorized | Ready to use |
|
||||
| `unauthorized` | USB debugging not authorized | Check phone for auth dialog |
|
||||
| `offline` | Connection issues | Unplug, replug, restart adb |
|
||||
| (empty) | Device not detected | Check USB cable, USB mode |
|
||||
|
||||
## Step 4: Build and Install the App
|
||||
|
||||
### Option A: Using Build Script (Recommended)
|
||||
|
||||
From the `test-apps/daily-notification-test` directory:
|
||||
|
||||
```bash
|
||||
# Build and run on connected device
|
||||
./scripts/build.sh --run-android
|
||||
```
|
||||
|
||||
### Option B: Manual Build
|
||||
|
||||
```bash
|
||||
# 1. Navigate to test app directory
|
||||
cd test-apps/daily-notification-test
|
||||
|
||||
# 2. Build web assets
|
||||
npm run build
|
||||
|
||||
# 3. Sync with Capacitor
|
||||
npm run cap:sync:android
|
||||
|
||||
# 4. Build APK
|
||||
cd android
|
||||
./gradlew :app:assembleDebug
|
||||
|
||||
# 5. Install on device
|
||||
adb install -r app/build/outputs/apk/debug/app-debug.apk
|
||||
|
||||
# 6. Launch app
|
||||
adb shell am start -n com.timesafari.dailynotification.test/.MainActivity
|
||||
```
|
||||
|
||||
### Option C: Using Capacitor CLI
|
||||
|
||||
```bash
|
||||
# Build, install, and launch in one command
|
||||
npx cap run android --target <device-id>
|
||||
|
||||
# Get device ID from:
|
||||
adb devices
|
||||
```
|
||||
|
||||
## Step 5: Configure Battery Optimization (Critical!)
|
||||
|
||||
**This is the most important step for notification testing.** Android OEMs aggressively kill background apps to save battery. Without proper configuration, your alarms and notifications may not fire.
|
||||
|
||||
### Disable Battery Optimization for Test App
|
||||
|
||||
1. **Settings** → **Apps** → **DailyNotification Test** (or your app name)
|
||||
2. **Battery** → **Unrestricted** or **Don't optimize**
|
||||
|
||||
### Manufacturer-Specific Settings
|
||||
|
||||
#### Samsung (One UI)
|
||||
|
||||
1. **Settings** → **Battery** → **Background usage limits**
|
||||
2. Remove app from "Sleeping apps" and "Deep sleeping apps"
|
||||
3. Add app to "Never sleeping apps"
|
||||
|
||||
#### Xiaomi (MIUI)
|
||||
|
||||
1. **Settings** → **Apps** → **Manage apps** → Select app
|
||||
2. Enable **Autostart**
|
||||
3. **Battery saver** → **No restrictions**
|
||||
4. **Security** app → **Permissions** → **Autostart** → Enable for app
|
||||
|
||||
#### OnePlus (OxygenOS)
|
||||
|
||||
1. **Settings** → **Battery** → **Battery optimization**
|
||||
2. Select app → **Don't optimize**
|
||||
3. **Settings** → **Apps** → Select app → **Advanced** → **Optimize battery usage** → Off
|
||||
|
||||
#### Huawei/Honor (EMUI)
|
||||
|
||||
1. **Settings** → **Battery** → **App launch**
|
||||
2. Disable automatic management for the app
|
||||
3. Enable all three toggles: Auto-launch, Secondary launch, Run in background
|
||||
|
||||
#### Oppo/Realme (ColorOS)
|
||||
|
||||
1. **Settings** → **Battery** → **More battery settings**
|
||||
2. **Optimize battery use** → Select app → **Don't optimize**
|
||||
3. Enable **Allow auto-start** and **Allow background activity**
|
||||
|
||||
### Verify Battery Settings
|
||||
|
||||
```bash
|
||||
# Check if app is whitelisted from battery optimization
|
||||
adb shell dumpsys deviceidle whitelist
|
||||
|
||||
# Should include your package name
|
||||
```
|
||||
|
||||
## Step 6: Monitor Logs
|
||||
|
||||
### Real-time Log Streaming
|
||||
|
||||
```bash
|
||||
# All logs from the app
|
||||
adb logcat | grep -E "DailyNotification|Capacitor|Console"
|
||||
|
||||
# Specific tags only
|
||||
adb logcat -s "DailyNotification" "Capacitor" "Console"
|
||||
|
||||
# Clear logs and start fresh
|
||||
adb logcat -c && adb logcat -s "DailyNotification"
|
||||
```
|
||||
|
||||
### Filter by Log Level
|
||||
|
||||
```bash
|
||||
# Errors only
|
||||
adb logcat *:E | grep DailyNotification
|
||||
|
||||
# Warnings and above
|
||||
adb logcat *:W | grep DailyNotification
|
||||
|
||||
# Verbose (all levels)
|
||||
adb logcat *:V | grep DailyNotification
|
||||
```
|
||||
|
||||
### Save Logs to File
|
||||
|
||||
```bash
|
||||
# Stream logs to file
|
||||
adb logcat -s "DailyNotification" > device_logs.txt
|
||||
|
||||
# Press Ctrl+C to stop
|
||||
```
|
||||
|
||||
### Check Alarm Scheduling
|
||||
|
||||
```bash
|
||||
# View scheduled alarms (requires root or debuggable build)
|
||||
adb shell dumpsys alarm | grep -A 5 "com.timesafari"
|
||||
|
||||
# View alarm statistics
|
||||
adb shell dumpsys alarm | grep -i "daily"
|
||||
```
|
||||
|
||||
## Step 7: Testing Notification Features
|
||||
|
||||
### Test Immediate Notification
|
||||
|
||||
1. Open the app
|
||||
2. Navigate to notification testing section
|
||||
3. Trigger an immediate notification
|
||||
4. Verify it appears in the notification tray
|
||||
|
||||
### Test Scheduled Notification
|
||||
|
||||
1. Schedule a notification for 1-2 minutes in the future
|
||||
2. Lock the phone or put app in background
|
||||
3. Wait for notification to fire
|
||||
4. Check logs if notification doesn't appear
|
||||
|
||||
### Test Alarm Persistence
|
||||
|
||||
1. Schedule a notification
|
||||
2. Reboot the device:
|
||||
```bash
|
||||
adb reboot
|
||||
```
|
||||
3. After reboot, check if alarm was restored:
|
||||
```bash
|
||||
adb shell dumpsys alarm | grep -A 5 "com.timesafari"
|
||||
```
|
||||
|
||||
### Test Force Stop Recovery
|
||||
|
||||
1. Schedule a notification
|
||||
2. Force stop the app:
|
||||
```bash
|
||||
adb shell am force-stop com.timesafari.dailynotification.test
|
||||
```
|
||||
3. Check if alarms are recovered (implementation dependent)
|
||||
|
||||
## Complete Command Sequence
|
||||
|
||||
### Quick Start (Copy-Paste Ready)
|
||||
|
||||
```bash
|
||||
# 1. Verify device connection
|
||||
adb devices
|
||||
|
||||
# 2. Navigate to test app
|
||||
cd test-apps/daily-notification-test
|
||||
|
||||
# 3. Build everything
|
||||
npm run build
|
||||
npm run cap:sync:android
|
||||
|
||||
# 4. Build and install APK
|
||||
cd android
|
||||
./gradlew :app:assembleDebug
|
||||
adb install -r app/build/outputs/apk/debug/app-debug.apk
|
||||
|
||||
# 5. Launch app
|
||||
adb shell am start -n com.timesafari.dailynotification.test/.MainActivity
|
||||
|
||||
# 6. Monitor logs (in separate terminal)
|
||||
adb logcat -s "DailyNotification" "Capacitor" "Console"
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Device Not Detected
|
||||
|
||||
```bash
|
||||
# Restart ADB server
|
||||
adb kill-server
|
||||
adb start-server
|
||||
adb devices
|
||||
|
||||
# Check USB connection
|
||||
# - Try different USB cable (use data cable, not charge-only)
|
||||
# - Try different USB port
|
||||
# - Check USB mode on phone (should be File transfer, not Charge only)
|
||||
```
|
||||
|
||||
### "Unauthorized" Device
|
||||
|
||||
```bash
|
||||
# Revoke USB debugging authorizations on phone:
|
||||
# Settings → Developer options → Revoke USB debugging authorizations
|
||||
|
||||
# Then reconnect and re-authorize
|
||||
adb kill-server
|
||||
adb start-server
|
||||
# Accept authorization dialog on phone
|
||||
```
|
||||
|
||||
### APK Installation Fails
|
||||
|
||||
```bash
|
||||
# Error: INSTALL_FAILED_UPDATE_INCOMPATIBLE
|
||||
# Solution: Uninstall existing app first
|
||||
adb uninstall com.timesafari.dailynotification.test
|
||||
adb install app/build/outputs/apk/debug/app-debug.apk
|
||||
|
||||
# Error: INSTALL_FAILED_USER_RESTRICTED
|
||||
# Solution: Enable "Install via USB" in Developer options
|
||||
```
|
||||
|
||||
### Notifications Not Appearing
|
||||
|
||||
1. **Check notification permissions:**
|
||||
```bash
|
||||
adb shell dumpsys notification | grep -A 10 "com.timesafari"
|
||||
```
|
||||
|
||||
2. **Check battery optimization:**
|
||||
- Ensure app is set to "Unrestricted" or "Don't optimize"
|
||||
- Check manufacturer-specific settings (see Step 5)
|
||||
|
||||
3. **Check Do Not Disturb:**
|
||||
- Ensure DND is off, or app is allowed through DND
|
||||
|
||||
4. **Check notification channel:**
|
||||
```bash
|
||||
adb shell dumpsys notification | grep -B 5 -A 10 "channel"
|
||||
```
|
||||
|
||||
### Alarms Not Firing
|
||||
|
||||
1. **Check if alarms are scheduled:**
|
||||
```bash
|
||||
adb shell dumpsys alarm | grep -A 10 "com.timesafari"
|
||||
```
|
||||
|
||||
2. **Check Doze mode:**
|
||||
```bash
|
||||
# Check current Doze state
|
||||
adb shell dumpsys deviceidle
|
||||
|
||||
# Force device out of Doze for testing
|
||||
adb shell dumpsys deviceidle unforce
|
||||
```
|
||||
|
||||
3. **Check exact alarm permission (Android 12+):**
|
||||
```bash
|
||||
adb shell appops get com.timesafari.dailynotification.test SCHEDULE_EXACT_ALARM
|
||||
```
|
||||
|
||||
### Build Failures
|
||||
|
||||
```bash
|
||||
# Clean build
|
||||
cd android
|
||||
./gradlew clean
|
||||
./gradlew :app:assembleDebug
|
||||
|
||||
# If still failing, clean Gradle cache
|
||||
rm -rf ~/.gradle/caches
|
||||
./gradlew :app:assembleDebug
|
||||
```
|
||||
|
||||
## Benefits of Physical Device Testing
|
||||
|
||||
### Advantages Over Emulator
|
||||
|
||||
- ✅ **Accurate notification timing** — Real hardware scheduler behavior
|
||||
- ✅ **Real battery optimization** — Test against actual OEM restrictions
|
||||
- ✅ **True Doze mode** — Emulators simulate but don't fully replicate
|
||||
- ✅ **Boot receiver testing** — Actual device reboot behavior
|
||||
- ✅ **Performance metrics** — Real CPU/memory usage
|
||||
- ✅ **User experience** — How notifications actually feel
|
||||
|
||||
### When to Use Physical Device
|
||||
|
||||
- **Final validation** — Before release
|
||||
- **Notification timing tests** — Alarm accuracy verification
|
||||
- **Battery impact testing** — Real power consumption
|
||||
- **Reboot persistence tests** — Boot receiver validation
|
||||
- **OEM-specific testing** — Samsung, Xiaomi, etc. quirks
|
||||
|
||||
### When Emulator is Sufficient
|
||||
|
||||
- **Basic functionality** — Core feature development
|
||||
- **UI testing** — Layout and interaction testing
|
||||
- **Quick iteration** — Fast build-test cycles
|
||||
- **CI/CD pipelines** — Automated testing
|
||||
|
||||
## Multiple Device Management
|
||||
|
||||
### List All Connected Devices
|
||||
|
||||
```bash
|
||||
adb devices -l
|
||||
|
||||
# Example output:
|
||||
# ABC123DEF456 device usb:1-1 product:walleye model:Pixel_2 device:walleye
|
||||
# XYZ789GHI012 device usb:1-2 product:star2lte model:SM_G965F device:star2lte
|
||||
```
|
||||
|
||||
### Target Specific Device
|
||||
|
||||
```bash
|
||||
# Install on specific device
|
||||
adb -s ABC123DEF456 install -r app/build/outputs/apk/debug/app-debug.apk
|
||||
|
||||
# View logs from specific device
|
||||
adb -s ABC123DEF456 logcat -s "DailyNotification"
|
||||
|
||||
# Launch app on specific device
|
||||
adb -s ABC123DEF456 shell am start -n com.timesafari.dailynotification.test/.MainActivity
|
||||
```
|
||||
|
||||
## Wireless ADB (Optional)
|
||||
|
||||
For cable-free development after initial setup:
|
||||
|
||||
```bash
|
||||
# 1. Connect device via USB first
|
||||
# 2. Enable TCP/IP mode on port 5555
|
||||
adb tcpip 5555
|
||||
|
||||
# 3. Find device IP (Settings → About phone → Status → IP address)
|
||||
# Or:
|
||||
adb shell ip addr show wlan0 | grep inet
|
||||
|
||||
# 4. Disconnect USB and connect wirelessly
|
||||
adb connect 192.168.1.100:5555
|
||||
|
||||
# 5. Verify connection
|
||||
adb devices
|
||||
# Should show: 192.168.1.100:5555 device
|
||||
```
|
||||
|
||||
**Note:** Wireless ADB is slower than USB and may disconnect. Use USB for large APK transfers.
|
||||
|
||||
## Next Steps
|
||||
|
||||
### Testing Workflow
|
||||
|
||||
1. **Build** → Make changes, rebuild APK
|
||||
2. **Install** → Push to device with `adb install -r`
|
||||
3. **Test** → Exercise notification features
|
||||
4. **Monitor** → Watch logs for issues
|
||||
5. **Iterate** → Fix and repeat
|
||||
|
||||
### Recommended Test Sequence
|
||||
|
||||
1. ✅ Immediate notification display
|
||||
2. ✅ Scheduled notification (1-2 min delay)
|
||||
3. ✅ App backgrounded notification
|
||||
4. ✅ Screen off notification
|
||||
5. ✅ Device reboot alarm persistence
|
||||
6. ✅ Force stop recovery (if implemented)
|
||||
7. ✅ Battery optimization scenarios
|
||||
|
||||
---
|
||||
|
||||
**Physical device testing is essential for production-quality notification behavior.** While emulators are great for development, only real hardware reveals the true behavior of Android's notification and alarm systems. 📱
|
||||
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'DailyNotificationPlugin'
|
||||
s.version = '1.0.0'
|
||||
s.version = '1.2.1'
|
||||
s.summary = 'Daily Notification Plugin for Capacitor'
|
||||
s.license = 'MIT'
|
||||
s.homepage = 'https://github.com/timesafari/daily-notification-plugin'
|
||||
|
||||
@@ -1118,12 +1118,14 @@ public class DailyNotificationPlugin: CAPPlugin {
|
||||
let sound = call.getBool("sound", true)
|
||||
let url = call.getString("url")
|
||||
let priority = call.getString("priority") ?? "default"
|
||||
let rawRollover = call.getInt("rolloverIntervalMinutes") ?? 0
|
||||
let rolloverIntervalMinutes = rawRollover > 0 ? rawRollover : nil
|
||||
|
||||
// Calculate scheduled time (next occurrence at specified hour:minute)
|
||||
let scheduledTime = calculateNextScheduledTime(hour: hour, minute: minute)
|
||||
let fetchedAt = Int64(Date().timeIntervalSince1970 * 1000) // Current time in milliseconds
|
||||
|
||||
// Create notification content
|
||||
// Create notification content (persist rollover interval for dev/testing; survives app restart)
|
||||
let content = NotificationContent(
|
||||
id: "daily_\(Date().timeIntervalSince1970)",
|
||||
title: title,
|
||||
@@ -1132,7 +1134,8 @@ public class DailyNotificationPlugin: CAPPlugin {
|
||||
fetchedAt: fetchedAt,
|
||||
url: url,
|
||||
payload: nil,
|
||||
etag: nil
|
||||
etag: nil,
|
||||
rolloverIntervalMinutes: rolloverIntervalMinutes
|
||||
)
|
||||
|
||||
// Delegate to ScheduleHelper for orchestration
|
||||
|
||||
@@ -382,55 +382,24 @@ class DailyNotificationScheduler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate next scheduled time from current scheduled time (24 hours later, DST-safe)
|
||||
*
|
||||
* Matches Android calculateNextScheduledTime() functionality
|
||||
* Handles DST transitions automatically using Calendar
|
||||
*
|
||||
* @param currentScheduledTime Current scheduled time in milliseconds
|
||||
* @return Next scheduled time in milliseconds (24 hours later)
|
||||
*
|
||||
* TESTING: To test with shorter intervals (e.g., 2 minutes), change:
|
||||
* - Line ~404: `.hour, value: 24` → `.minute, value: 2`
|
||||
* - Line ~407: `(24 * 60 * 60 * 1000)` → `(2 * 60 * 1000)`
|
||||
* Calculate next scheduled time from current (24h or rollover interval minutes). DST-safe.
|
||||
* When rolloverIntervalMinutes > 0 (dev/testing), adds that many minutes; otherwise adds 24 hours.
|
||||
*/
|
||||
func calculateNextScheduledTime(_ currentScheduledTime: Int64) -> Int64 {
|
||||
func calculateNextScheduledTime(_ currentScheduledTime: Int64, rolloverIntervalMinutes: Int? = nil) -> Int64 {
|
||||
let calendar = Calendar.current
|
||||
let currentDate = Date(timeIntervalSince1970: Double(currentScheduledTime) / 1000.0)
|
||||
let currentTimeStr = formatTime(currentScheduledTime)
|
||||
|
||||
// Add 24 hours (handles DST transitions automatically)
|
||||
// TESTING: Change `.hour, value: 24` to `.minute, value: 2` for 2-minute testing
|
||||
guard let nextDate = calendar.date(byAdding: .hour, value: 24, to: currentDate) else {
|
||||
// Fallback to simple 24-hour addition if calendar calculation fails
|
||||
// TESTING: Change `(24 * 60 * 60 * 1000)` to `(2 * 60 * 1000)` for 2-minute testing
|
||||
let fallbackTime = currentScheduledTime + (24 * 60 * 60 * 1000)
|
||||
let fallbackTimeStr = formatTime(fallbackTime)
|
||||
NSLog("DNP-ROLLOVER: DST_CALC_FAILED current=\(currentTimeStr) using_fallback=\(fallbackTimeStr)")
|
||||
print("DNP-ROLLOVER: DST_CALC_FAILED current=\(currentTimeStr) using_fallback=\(fallbackTimeStr)")
|
||||
let addMinutes = (rolloverIntervalMinutes ?? 0) > 0 ? rolloverIntervalMinutes! : (24 * 60)
|
||||
guard let nextDate = calendar.date(byAdding: .minute, value: addMinutes, to: currentDate) else {
|
||||
let fallbackTime = currentScheduledTime + (Int64(addMinutes) * 60 * 1000)
|
||||
NSLog("DNP-ROLLOVER: DST_CALC_FAILED current=\(currentTimeStr) using_fallback add_minutes=\(addMinutes)")
|
||||
return fallbackTime
|
||||
}
|
||||
|
||||
let nextTime = Int64(nextDate.timeIntervalSince1970 * 1000)
|
||||
let nextTimeStr = formatTime(nextTime)
|
||||
|
||||
// Validate: Log DST transitions for debugging
|
||||
let currentHour = calendar.component(.hour, from: currentDate)
|
||||
let currentMinute = calendar.component(.minute, from: currentDate)
|
||||
let nextHour = calendar.component(.hour, from: nextDate)
|
||||
let nextMinute = calendar.component(.minute, from: nextDate)
|
||||
|
||||
if currentHour != nextHour || currentMinute != nextMinute {
|
||||
NSLog("DNP-ROLLOVER: DST_TRANSITION current=\(currentHour):\(String(format: "%02d", currentMinute)) next=\(nextHour):\(String(format: "%02d", nextMinute))")
|
||||
print("DNP-ROLLOVER: DST_TRANSITION current=\(currentHour):\(String(format: "%02d", currentMinute)) next=\(nextHour):\(String(format: "%02d", nextMinute))")
|
||||
}
|
||||
|
||||
// Log the calculation result
|
||||
let timeDiffMs = nextTime - currentScheduledTime
|
||||
let timeDiffHours = Double(timeDiffMs) / 1000.0 / 60.0 / 60.0
|
||||
NSLog("DNP-ROLLOVER: CALC_NEXT current=\(currentTimeStr) next=\(nextTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
|
||||
print("DNP-ROLLOVER: CALC_NEXT current=\(currentTimeStr) next=\(nextTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
|
||||
|
||||
NSLog("DNP-ROLLOVER: CALC_NEXT current=\(currentTimeStr) next=\(nextTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours)) rollover_min=\(rolloverIntervalMinutes ?? 0)")
|
||||
return nextTime
|
||||
}
|
||||
|
||||
@@ -473,16 +442,16 @@ class DailyNotificationScheduler {
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate next occurrence using DST-safe calculation
|
||||
var nextScheduledTime = calculateNextScheduledTime(content.scheduledTime)
|
||||
// Calculate next occurrence (use stored rollover interval for dev/testing, else 24h)
|
||||
let rolloverMin = (content.rolloverIntervalMinutes ?? 0) > 0 ? content.rolloverIntervalMinutes : nil
|
||||
var nextScheduledTime = calculateNextScheduledTime(content.scheduledTime, rolloverIntervalMinutes: rolloverMin)
|
||||
|
||||
// If next scheduled time is in the past, keep calculating forward until we get a future time
|
||||
// This handles cases where the notification fired more than 2 minutes ago
|
||||
while nextScheduledTime < currentTime {
|
||||
let nextTimeStr = formatTime(nextScheduledTime)
|
||||
NSLog("DNP-ROLLOVER: NEXT_IN_PAST id=\(content.id) next_time=\(nextTimeStr) current_time=\(currentTimeStr), calculating forward")
|
||||
print("DNP-ROLLOVER: NEXT_IN_PAST id=\(content.id) next_time=\(nextTimeStr) current_time=\(currentTimeStr), calculating forward")
|
||||
nextScheduledTime = calculateNextScheduledTime(nextScheduledTime)
|
||||
nextScheduledTime = calculateNextScheduledTime(nextScheduledTime, rolloverIntervalMinutes: rolloverMin)
|
||||
}
|
||||
|
||||
let nextScheduledTimeStr = formatTime(nextScheduledTime)
|
||||
@@ -539,13 +508,14 @@ class DailyNotificationScheduler {
|
||||
let nextId = "daily_rollover_\(Int64(Date().timeIntervalSince1970 * 1000))"
|
||||
let nextContent = NotificationContent(
|
||||
id: nextId,
|
||||
title: content.title, // Will be updated by prefetch
|
||||
body: content.body, // Will be updated by prefetch
|
||||
title: content.title,
|
||||
body: content.body,
|
||||
scheduledTime: nextScheduledTime,
|
||||
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
|
||||
url: content.url,
|
||||
payload: content.payload,
|
||||
etag: content.etag
|
||||
etag: content.etag,
|
||||
rolloverIntervalMinutes: content.rolloverIntervalMinutes
|
||||
)
|
||||
|
||||
// Schedule the next notification
|
||||
|
||||
@@ -27,6 +27,8 @@ class NotificationContent: Codable {
|
||||
let url: String?
|
||||
let payload: [String: Any]?
|
||||
let etag: String?
|
||||
/** When > 0, next occurrence is this many minutes after trigger (dev/testing). Nil/0 = 24h. Persisted for rollover and recovery. */
|
||||
var rolloverIntervalMinutes: Int?
|
||||
|
||||
// Phase 2: Delivery tracking properties
|
||||
var deliveryStatus: String? // e.g., "scheduled", "delivered", "missed", "error"
|
||||
@@ -43,6 +45,7 @@ class NotificationContent: Codable {
|
||||
case url
|
||||
case payload
|
||||
case etag
|
||||
case rolloverIntervalMinutes
|
||||
case deliveryStatus
|
||||
case lastDeliveryAttempt
|
||||
}
|
||||
@@ -64,6 +67,7 @@ class NotificationContent: Codable {
|
||||
payload = nil
|
||||
}
|
||||
etag = try container.decodeIfPresent(String.self, forKey: .etag)
|
||||
rolloverIntervalMinutes = try container.decodeIfPresent(Int.self, forKey: .rolloverIntervalMinutes)
|
||||
deliveryStatus = try container.decodeIfPresent(String.self, forKey: .deliveryStatus)
|
||||
lastDeliveryAttempt = try container.decodeIfPresent(Int64.self, forKey: .lastDeliveryAttempt)
|
||||
}
|
||||
@@ -83,6 +87,7 @@ class NotificationContent: Codable {
|
||||
try container.encode(payloadString, forKey: .payload)
|
||||
}
|
||||
try container.encodeIfPresent(etag, forKey: .etag)
|
||||
try container.encodeIfPresent(rolloverIntervalMinutes, forKey: .rolloverIntervalMinutes)
|
||||
try container.encodeIfPresent(deliveryStatus, forKey: .deliveryStatus)
|
||||
try container.encodeIfPresent(lastDeliveryAttempt, forKey: .lastDeliveryAttempt)
|
||||
}
|
||||
@@ -100,20 +105,21 @@ class NotificationContent: Codable {
|
||||
* @param url URL for content fetching
|
||||
* @param payload Additional payload data
|
||||
* @param etag ETag for HTTP caching
|
||||
* @param rolloverIntervalMinutes When > 0, next occurrence is this many minutes after trigger (dev/testing). Nil/0 = 24h.
|
||||
* @param deliveryStatus Delivery status (optional, Phase 2)
|
||||
* @param lastDeliveryAttempt Last delivery attempt timestamp (optional, Phase 2)
|
||||
*/
|
||||
init(id: String,
|
||||
title: String?,
|
||||
body: String?,
|
||||
scheduledTime: Int64,
|
||||
fetchedAt: Int64,
|
||||
url: String?,
|
||||
payload: [String: Any]?,
|
||||
init(id: String,
|
||||
title: String?,
|
||||
body: String?,
|
||||
scheduledTime: Int64,
|
||||
fetchedAt: Int64,
|
||||
url: String?,
|
||||
payload: [String: Any]?,
|
||||
etag: String?,
|
||||
rolloverIntervalMinutes: Int? = nil,
|
||||
deliveryStatus: String? = nil,
|
||||
lastDeliveryAttempt: Int64? = nil) {
|
||||
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.body = body
|
||||
@@ -122,6 +128,7 @@ class NotificationContent: Codable {
|
||||
self.url = url
|
||||
self.payload = payload
|
||||
self.etag = etag
|
||||
self.rolloverIntervalMinutes = rolloverIntervalMinutes
|
||||
self.deliveryStatus = deliveryStatus
|
||||
self.lastDeliveryAttempt = lastDeliveryAttempt
|
||||
}
|
||||
@@ -259,6 +266,7 @@ class NotificationContent: Codable {
|
||||
lastDeliveryAttempt = nil
|
||||
}
|
||||
|
||||
let rollover = (dict["rolloverIntervalMinutes"] as? NSNumber)?.intValue
|
||||
return NotificationContent(
|
||||
id: id,
|
||||
title: dict["title"] as? String,
|
||||
@@ -268,6 +276,7 @@ class NotificationContent: Codable {
|
||||
url: dict["url"] as? String,
|
||||
payload: dict["payload"] as? [String: Any],
|
||||
etag: dict["etag"] as? String,
|
||||
rolloverIntervalMinutes: rollover,
|
||||
deliveryStatus: dict["deliveryStatus"] as? String,
|
||||
lastDeliveryAttempt: lastDeliveryAttempt
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@timesafari/daily-notification-plugin",
|
||||
"version": "1.0.11",
|
||||
"version": "1.3.0",
|
||||
"description": "TimeSafari Daily Notification Plugin - Enterprise-grade daily notification functionality with dual scheduling, callback support, TTL-at-fire logic, and comprehensive observability across Mobile (Capacitor) and Desktop (Electron) platforms",
|
||||
"main": "dist/plugin.js",
|
||||
"module": "dist/esm/index.js",
|
||||
@@ -14,6 +14,7 @@
|
||||
"build:all": "npm run build:timesafari",
|
||||
"clean": "rimraf ./dist",
|
||||
"watch": "tsc --watch",
|
||||
"prepare": "npm run build",
|
||||
"prepublishOnly": "npm run build",
|
||||
"test": "jest",
|
||||
"test:workspaces": "npm test --workspaces",
|
||||
@@ -99,6 +100,10 @@
|
||||
},
|
||||
"files": [
|
||||
"dist/",
|
||||
"src/",
|
||||
"rollup.config.js",
|
||||
"tsconfig.json",
|
||||
"scripts/",
|
||||
"android/",
|
||||
"ios/Plugin/",
|
||||
"ios/Tests/",
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* Aligned with Android implementation and test requirements
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.0.11 (see package.json for source of truth)
|
||||
* @version 1.3.0 (see package.json for source of truth)
|
||||
*/
|
||||
|
||||
// Import SPI types from content-fetcher.ts
|
||||
@@ -82,6 +82,12 @@ export interface NotificationOptions {
|
||||
retryInterval?: number;
|
||||
offlineFallback?: boolean;
|
||||
headers?: Record<string, string>;
|
||||
/**
|
||||
* Optional rollover interval in minutes for dev/testing.
|
||||
* When set (e.g. 10), the next occurrence is scheduled this many minutes after the current trigger
|
||||
* instead of 24 hours. Persisted with the schedule so it survives reboot. When absent or <= 0, use 24h.
|
||||
*/
|
||||
rolloverIntervalMinutes?: number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Provides structured logging, event codes, and health monitoring
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
* @version 1.3.0
|
||||
*/
|
||||
|
||||
import {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* This implementation provides clear error messages for all methods.
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.0.0
|
||||
* @version 1.3.0
|
||||
*/
|
||||
|
||||
import type {
|
||||
|
||||
@@ -196,22 +196,27 @@
|
||||
console.log('[UI Refresh] Calling getNotificationStatus()...');
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
console.log('[UI Refresh] getNotificationStatus result:', {
|
||||
console.log('[UI Refresh] getNotificationStatus result:', JSON.stringify({
|
||||
nextNotificationTime: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
isEnabled: result.isEnabled,
|
||||
pending: result.pending,
|
||||
lastNotificationTime: result.lastNotificationTime
|
||||
});
|
||||
lastNotificationTime: result.lastNotificationTime,
|
||||
lastNotificationTimeDate: result.lastNotificationTime ? new Date(result.lastNotificationTime).toISOString() : null
|
||||
}, null, 2));
|
||||
|
||||
const nextTime = formatDateTimeNormalized(result.nextNotificationTime);
|
||||
const hasSchedules = result.isEnabled || (result.pending && result.pending > 0);
|
||||
const statusIcon = hasSchedules ? '✅' : '⏸️';
|
||||
|
||||
console.log('[UI Refresh] Updating UI:', {
|
||||
console.log('[UI Refresh] Updating UI:', JSON.stringify({
|
||||
nextTime: nextTime,
|
||||
nextNotificationTimeRaw: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
hasSchedules: hasSchedules,
|
||||
statusIcon: statusIcon
|
||||
});
|
||||
statusIcon: statusIcon,
|
||||
pending: result.pending
|
||||
}, null, 2));
|
||||
|
||||
pluginStatusContent.innerHTML = `${statusIcon} Active Schedules: ${hasSchedules ? 'Yes' : 'No'}<br>
|
||||
📅 Next Notification: ${nextTime}<br>
|
||||
@@ -341,6 +346,60 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Load configuration status (plugin settings and native fetcher)
|
||||
function loadConfigurationStatus() {
|
||||
console.log('[Config Check] Checking configuration status...');
|
||||
const configStatus = document.getElementById('configStatus');
|
||||
const fetcherStatus = document.getElementById('fetcherStatus');
|
||||
|
||||
if (!window.DailyNotification) {
|
||||
console.warn('[Config Check] DailyNotification plugin not available');
|
||||
configStatus.innerHTML = '❌ Plugin unavailable';
|
||||
fetcherStatus.innerHTML = '❌ Plugin unavailable';
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if plugin settings are configured
|
||||
// Plugin settings are stored internally, so we check by trying to get status
|
||||
// For now, we'll check if native fetcher config exists as a proxy
|
||||
// TODO: Add explicit plugin settings check method
|
||||
window.DailyNotification.getConfig({ key: 'native_fetcher_config' })
|
||||
.then(result => {
|
||||
console.log('[Config Check] Native fetcher config result:', JSON.stringify(result));
|
||||
if (result && result.config && result.config.configValue) {
|
||||
try {
|
||||
const configValue = JSON.parse(result.config.configValue);
|
||||
if (configValue.apiBaseUrl && configValue.apiBaseUrl.length > 0) {
|
||||
console.log('[Config Check] ✅ Native fetcher is configured');
|
||||
fetcherStatus.innerHTML = '✅ Configured';
|
||||
} else {
|
||||
console.log('[Config Check] ⚠️ Native fetcher config exists but is empty');
|
||||
fetcherStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[Config Check] Failed to parse config value:', e);
|
||||
fetcherStatus.innerHTML = '❌ Error';
|
||||
}
|
||||
} else {
|
||||
console.log('[Config Check] ❌ Native fetcher config not found');
|
||||
fetcherStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
|
||||
// For plugin settings, we assume configured if native fetcher is configured
|
||||
// This is a heuristic - in production, add explicit check
|
||||
if (fetcherStatus.innerHTML.includes('✅')) {
|
||||
configStatus.innerHTML = '✅ Configured';
|
||||
} else {
|
||||
configStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[Config Check] Failed to check configuration:', error);
|
||||
configStatus.innerHTML = '❌ Error';
|
||||
fetcherStatus.innerHTML = '❌ Error';
|
||||
});
|
||||
}
|
||||
|
||||
function loadChannelStatus() {
|
||||
const channelStatus = document.getElementById('channelStatus');
|
||||
|
||||
@@ -472,11 +531,14 @@
|
||||
console.log('[Poll] checkNotificationDelivery called');
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
console.log('[Poll] Status check result:', {
|
||||
console.log('[Poll] Status check result:', JSON.stringify({
|
||||
nextNotificationTime: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
lastNotificationTime: result.lastNotificationTime,
|
||||
lastKnownNextNotificationTime: lastKnownNextNotificationTime
|
||||
});
|
||||
lastNotificationTimeDate: result.lastNotificationTime ? new Date(result.lastNotificationTime).toISOString() : null,
|
||||
lastKnownNextNotificationTime: lastKnownNextNotificationTime,
|
||||
lastKnownNextNotificationTimeDate: lastKnownNextNotificationTime ? new Date(lastKnownNextNotificationTime).toISOString() : null
|
||||
}, null, 2));
|
||||
|
||||
// Check for notification delivery
|
||||
if (result.lastNotificationTime) {
|
||||
@@ -520,11 +582,13 @@
|
||||
|
||||
// Detect if nextNotificationTime changed (rollover occurred)
|
||||
const currentNextTime = result.nextNotificationTime;
|
||||
console.log('[Poll] Comparing nextNotificationTime:', {
|
||||
console.log('[Poll] Comparing nextNotificationTime:', JSON.stringify({
|
||||
current: currentNextTime,
|
||||
currentDate: currentNextTime ? new Date(currentNextTime).toISOString() : null,
|
||||
lastKnown: lastKnownNextNotificationTime,
|
||||
lastKnownDate: lastKnownNextNotificationTime ? new Date(lastKnownNextNotificationTime).toISOString() : null,
|
||||
changed: currentNextTime && currentNextTime !== lastKnownNextNotificationTime
|
||||
});
|
||||
}, null, 2));
|
||||
|
||||
if (currentNextTime && currentNextTime !== lastKnownNextNotificationTime) {
|
||||
if (lastKnownNextNotificationTime !== null) {
|
||||
@@ -558,11 +622,12 @@
|
||||
// Load plugin status automatically on page load
|
||||
window.addEventListener('load', () => {
|
||||
console.log('Page loaded, loading plugin status...');
|
||||
// Small delay to ensure Capacitor is ready
|
||||
setTimeout(() => {
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
// Small delay to ensure Capacitor is ready
|
||||
setTimeout(() => {
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
loadConfigurationStatus();
|
||||
|
||||
// Initialize last known next notification time
|
||||
if (window.DailyNotification) {
|
||||
@@ -580,6 +645,57 @@
|
||||
}, 500);
|
||||
});
|
||||
|
||||
// Refresh UI when app comes back to foreground (after force-stop, app resume, etc.)
|
||||
// This ensures the UI updates after recovery from force-stop
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (!document.hidden) {
|
||||
console.log('[Visibility] App became visible, refreshing UI status...');
|
||||
// Small delay to allow recovery to complete
|
||||
setTimeout(() => {
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
loadConfigurationStatus();
|
||||
|
||||
// Also check for recent notifications that might have been missed
|
||||
if (window.DailyNotification) {
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
// Check if a notification was received recently (within last 2 minutes)
|
||||
if (result.lastNotificationTime) {
|
||||
const lastTime = new Date(result.lastNotificationTime);
|
||||
const now = new Date();
|
||||
const timeDiff = now - lastTime;
|
||||
|
||||
if (timeDiff > 0 && timeDiff < 120000) {
|
||||
console.log('[Visibility] Recent notification detected, showing indicator');
|
||||
const indicator = document.getElementById('notificationReceivedIndicator');
|
||||
const timeSpan = document.getElementById('notificationReceivedTime');
|
||||
|
||||
if (indicator && timeSpan) {
|
||||
indicator.style.display = 'block';
|
||||
lastTime.setSeconds(0, 0);
|
||||
timeSpan.textContent = `Received at ${lastTime.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true })}`;
|
||||
|
||||
// Hide after 30 seconds
|
||||
setTimeout(() => {
|
||||
indicator.style.display = 'none';
|
||||
}, 30000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update last known next notification time
|
||||
lastKnownNextNotificationTime = result.nextNotificationTime;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[Visibility] Failed to get notification status:', error);
|
||||
});
|
||||
}
|
||||
}, 1000); // Wait 1 second for recovery to complete
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
console.log('Functions attached to window:', {
|
||||
|
||||
@@ -824,18 +824,36 @@ main() {
|
||||
info "Post-rollover alarm time: ${post_rollover_alarm_time} (normalized)"
|
||||
|
||||
# Verify alarm time changed (rollover occurred)
|
||||
# Compare dates only (YYYY-MM-DD) to detect day change
|
||||
if [ -n "${initial_alarm_time}" ] && [ -n "${post_rollover_alarm_time}" ]; then
|
||||
local initial_date=$(echo "${initial_alarm_time}" | cut -d' ' -f1)
|
||||
# Compare alarm date to current date - if alarm is scheduled for tomorrow or later, rollover worked
|
||||
if [ -n "${post_rollover_alarm_time}" ]; then
|
||||
local current_date=$(date +%Y-%m-%d)
|
||||
local post_date=$(echo "${post_rollover_alarm_time}" | cut -d' ' -f1)
|
||||
|
||||
if [ "${initial_date}" != "${post_date}" ]; then
|
||||
ok "Alarm date changed: ${initial_alarm_time} → ${post_rollover_alarm_time}"
|
||||
rollover_verified=true
|
||||
# Compare dates: if alarm date is >= current date, it's scheduled for today or future (correct)
|
||||
# If we also have initial_alarm_time, check if it advanced
|
||||
if [ -n "${initial_alarm_time}" ]; then
|
||||
local initial_date=$(echo "${initial_alarm_time}" | cut -d' ' -f1)
|
||||
if [ "${initial_date}" != "${post_date}" ]; then
|
||||
ok "Alarm date changed: ${initial_alarm_time} → ${post_rollover_alarm_time}"
|
||||
rollover_verified=true
|
||||
elif [ "${post_date}" \> "${current_date}" ] || [ "${post_date}" = "${current_date}" ]; then
|
||||
# Alarm is scheduled for today or future - this is correct
|
||||
# If initial was also tomorrow, that's fine - rollover logs will confirm it occurred
|
||||
info "Alarm scheduled for ${post_date} (current: ${current_date}) - date unchanged from initial, checking rollover logs"
|
||||
# Don't set rollover_verified yet - let log check determine it
|
||||
else
|
||||
warn "Alarm date ${post_date} is in the past (current: ${current_date}) - rollover may have failed"
|
||||
rollover_verified=false
|
||||
fi
|
||||
else
|
||||
warn "Alarm date did NOT change: ${post_rollover_alarm_time} (same date as initial: ${initial_date})"
|
||||
warn "This indicates the notification did not fire and rollover did not occur"
|
||||
rollover_verified=false
|
||||
# No initial alarm time to compare, just check if it's scheduled for future
|
||||
if [ "${post_date}" \> "${current_date}" ] || [ "${post_date}" = "${current_date}" ]; then
|
||||
info "Alarm scheduled for ${post_date} (current: ${current_date}) - checking rollover logs for confirmation"
|
||||
# Don't set rollover_verified yet - let log check determine it
|
||||
else
|
||||
warn "Alarm date ${post_date} is in the past (current: ${current_date}) - rollover may have failed"
|
||||
rollover_verified=false
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
@@ -1203,43 +1221,62 @@ main() {
|
||||
|
||||
evidence_block "test1_force_stop_recovery"
|
||||
|
||||
# Optional: Verify alarm fires (controlled by VERIFY_FIRE flag)
|
||||
if [ "${VERIFY_FIRE}" = "true" ] && [ -n "${alarm_trigger_ms}" ] && [ "${goto_test1_end}" != "true" ]; then
|
||||
substep "Step 8: Verify alarm fires at scheduled time (optional)"
|
||||
|
||||
# Verify alarm fires if it's scheduled within reasonable time window (< 5 minutes)
|
||||
# This ensures restored alarms actually work, not just that they were restored
|
||||
if [ -n "${alarm_trigger_ms}" ] && [ "${goto_test1_end}" != "true" ]; then
|
||||
local current_time_sec current_time_ms wait_ms wait_sec
|
||||
current_time_sec=$(get_current_time)
|
||||
current_time_ms=$((current_time_sec * 1000))
|
||||
wait_ms=$((alarm_trigger_ms - current_time_ms))
|
||||
|
||||
if [ "${wait_ms}" -lt 0 ]; then
|
||||
warn "Alarm time already passed (${wait_ms} ms ago); skipping fire verification"
|
||||
else
|
||||
wait_sec=$((wait_ms / 1000))
|
||||
# Auto-enable fire verification if alarm is within 5 minutes (300 seconds)
|
||||
# Or if VERIFY_FIRE is explicitly set to true
|
||||
local should_verify_fire=false
|
||||
if [ "${VERIFY_FIRE}" = "true" ]; then
|
||||
should_verify_fire=true
|
||||
elif [ "${wait_ms}" -gt 0 ] && [ "${wait_ms}" -lt 300000 ]; then
|
||||
# Alarm is in the future and within 5 minutes - auto-verify
|
||||
should_verify_fire=true
|
||||
fi
|
||||
|
||||
if [ "${should_verify_fire}" = "true" ]; then
|
||||
substep "Step 8: Verify restored alarm fires at scheduled time"
|
||||
set_test_context "phase1" "phase1_test1" "p1_t1_s6"
|
||||
|
||||
# Clamp upper bound to prevent accidentally waiting 30+ minutes
|
||||
if [ "${wait_sec}" -gt 600 ]; then
|
||||
warn "Alarm is >10 minutes away (${wait_sec}s); skipping fire verification"
|
||||
info "To test fire verification, schedule alarm closer to current time"
|
||||
if [ "${wait_ms}" -lt 0 ]; then
|
||||
step_warn "p1_t1_s6" "Alarm time already passed"
|
||||
warn "Alarm time already passed (${wait_ms} ms ago); cannot verify fire"
|
||||
else
|
||||
info "Alarm scheduled for: ${alarm_readable}"
|
||||
info "Current time: $(date -d "@${current_time_sec}" 2>/dev/null || echo "${current_time_sec}")"
|
||||
info "Waiting ~${wait_sec} seconds for alarm to fire..."
|
||||
wait_sec=$((wait_ms / 1000))
|
||||
|
||||
clear_logs
|
||||
sleep ${wait_sec}
|
||||
sleep 2
|
||||
|
||||
info "Checking logs for fired alarm..."
|
||||
local alarm_fired
|
||||
alarm_fired="$($ADB_BIN logcat -d | grep -E "DNP-RECEIVE|DNP-NOTIFY|DNP-WORK|Alarm fired|Notification displayed" | tail -10)"
|
||||
|
||||
if [ -n "${alarm_fired}" ]; then
|
||||
ok "Alarm fired! Logs:"
|
||||
echo "${alarm_fired}"
|
||||
# Clamp upper bound to prevent accidentally waiting too long
|
||||
if [ "${wait_sec}" -gt 600 ]; then
|
||||
step_warn "p1_t1_s6" "Alarm too far in future"
|
||||
warn "Alarm is >10 minutes away (${wait_sec}s); skipping fire verification"
|
||||
info "To test fire verification, schedule alarm closer to current time"
|
||||
else
|
||||
warn "No alarm fire logs found"
|
||||
info "Check notification tray manually or review recent logs"
|
||||
step_start "p1_t1_s6" "Waiting for restored alarm to fire"
|
||||
info "Restored alarm scheduled for: ${alarm_readable}"
|
||||
info "Current time: $(date -d "@${current_time_sec}" 2>/dev/null || echo "${current_time_sec}")"
|
||||
info "Waiting ~${wait_sec} seconds for restored alarm to fire..."
|
||||
|
||||
clear_logs
|
||||
sleep ${wait_sec}
|
||||
sleep 2
|
||||
|
||||
info "Checking logs for fired alarm..."
|
||||
local alarm_fired
|
||||
alarm_fired="$($ADB_BIN logcat -d | grep -E "DNP-RECEIVE|DNP-NOTIFY|DNP-WORK|Alarm fired|Notification displayed" | tail -10)"
|
||||
|
||||
if [ -n "${alarm_fired}" ]; then
|
||||
step_pass "p1_t1_s6" "Restored alarm fired successfully"
|
||||
ok "✅ Restored alarm fired! Logs:"
|
||||
echo "${alarm_fired}"
|
||||
else
|
||||
step_fail "p1_t1_s6" "Restored alarm did not fire"
|
||||
warn "⚠️ No alarm fire logs found - restored alarm may not have fired"
|
||||
info "Check notification tray manually or review recent logs"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -220,11 +220,23 @@ if ! npm run build; then
|
||||
fi
|
||||
log_info "Web assets built successfully"
|
||||
|
||||
# Step 2: Sync Capacitor
|
||||
# Step 2: Sync Capacitor (Android-only when building only Android to avoid iOS pod install failure)
|
||||
log_step "Syncing Capacitor with native projects..."
|
||||
if ! npm run cap:sync; then
|
||||
log_error "Capacitor sync failed"
|
||||
exit 1
|
||||
if [ "$BUILD_ALL" = true ]; then
|
||||
if ! npm run cap:sync; then
|
||||
log_error "Capacitor sync failed"
|
||||
exit 1
|
||||
fi
|
||||
elif [ "$BUILD_ANDROID" = true ]; then
|
||||
if ! npm run cap:sync:android; then
|
||||
log_error "Capacitor sync (Android) failed"
|
||||
exit 1
|
||||
fi
|
||||
elif [ "$BUILD_IOS" = true ]; then
|
||||
if ! npm run cap:sync:ios; then
|
||||
log_error "Capacitor sync (iOS) failed"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
log_info "Capacitor sync completed"
|
||||
|
||||
|
||||
163
www/index.html
163
www/index.html
@@ -196,22 +196,27 @@
|
||||
console.log('[UI Refresh] Calling getNotificationStatus()...');
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
console.log('[UI Refresh] getNotificationStatus result:', {
|
||||
console.log('[UI Refresh] getNotificationStatus result:', JSON.stringify({
|
||||
nextNotificationTime: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
isEnabled: result.isEnabled,
|
||||
pending: result.pending,
|
||||
lastNotificationTime: result.lastNotificationTime
|
||||
});
|
||||
lastNotificationTime: result.lastNotificationTime,
|
||||
lastNotificationTimeDate: result.lastNotificationTime ? new Date(result.lastNotificationTime).toISOString() : null
|
||||
}, null, 2));
|
||||
|
||||
const nextTime = formatDateTimeNormalized(result.nextNotificationTime);
|
||||
const hasSchedules = result.isEnabled || (result.pending && result.pending > 0);
|
||||
const statusIcon = hasSchedules ? '✅' : '⏸️';
|
||||
|
||||
console.log('[UI Refresh] Updating UI:', {
|
||||
console.log('[UI Refresh] Updating UI:', JSON.stringify({
|
||||
nextTime: nextTime,
|
||||
nextNotificationTimeRaw: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
hasSchedules: hasSchedules,
|
||||
statusIcon: statusIcon
|
||||
});
|
||||
statusIcon: statusIcon,
|
||||
pending: result.pending
|
||||
}, null, 2));
|
||||
|
||||
pluginStatusContent.innerHTML = `${statusIcon} Active Schedules: ${hasSchedules ? 'Yes' : 'No'}<br>
|
||||
📅 Next Notification: ${nextTime}<br>
|
||||
@@ -341,6 +346,68 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Load configuration status (plugin settings and native fetcher)
|
||||
function loadConfigurationStatus() {
|
||||
console.log('[Config Check] Checking configuration status...');
|
||||
const configStatus = document.getElementById('configStatus');
|
||||
const fetcherStatus = document.getElementById('fetcherStatus');
|
||||
|
||||
if (!window.DailyNotification) {
|
||||
console.warn('[Config Check] DailyNotification plugin not available');
|
||||
configStatus.innerHTML = '❌ Plugin unavailable';
|
||||
fetcherStatus.innerHTML = '❌ Plugin unavailable';
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if plugin settings are configured
|
||||
// Plugin settings are stored internally, so we check by trying to get status
|
||||
// For now, we'll check if native fetcher config exists as a proxy
|
||||
// TODO: Add explicit plugin settings check method
|
||||
window.DailyNotification.getConfig({ key: 'native_fetcher_config' })
|
||||
.then(result => {
|
||||
console.log('[Config Check] Native fetcher config result:', JSON.stringify(result));
|
||||
if (result && result.config && result.config.configValue) {
|
||||
try {
|
||||
const configValue = JSON.parse(result.config.configValue);
|
||||
if (configValue.apiBaseUrl && configValue.apiBaseUrl.length > 0) {
|
||||
console.log('[Config Check] ✅ Native fetcher is configured');
|
||||
fetcherStatus.innerHTML = '✅ Configured';
|
||||
} else {
|
||||
console.log('[Config Check] ⚠️ Native fetcher config exists but is empty');
|
||||
fetcherStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[Config Check] Failed to parse config value:', e);
|
||||
fetcherStatus.innerHTML = '❌ Error';
|
||||
}
|
||||
} else {
|
||||
console.log('[Config Check] ❌ Native fetcher config not found in database');
|
||||
console.log('[Config Check] This may be normal after app uninstall/reinstall (database wiped)');
|
||||
fetcherStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
|
||||
// For plugin settings, we assume configured if native fetcher is configured
|
||||
// This is a heuristic - in production, add explicit check
|
||||
if (fetcherStatus.innerHTML.includes('✅')) {
|
||||
configStatus.innerHTML = '✅ Configured';
|
||||
} else {
|
||||
configStatus.innerHTML = '❌ Not configured';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[Config Check] Failed to check configuration:', error);
|
||||
// Don't show error if database might not be ready yet (recovery in progress)
|
||||
if (error.message && error.message.includes('database')) {
|
||||
console.log('[Config Check] Database may not be ready yet, will retry...');
|
||||
fetcherStatus.innerHTML = '⏳ Checking...';
|
||||
configStatus.innerHTML = '⏳ Checking...';
|
||||
} else {
|
||||
configStatus.innerHTML = '❌ Error';
|
||||
fetcherStatus.innerHTML = '❌ Error';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadChannelStatus() {
|
||||
const channelStatus = document.getElementById('channelStatus');
|
||||
|
||||
@@ -472,11 +539,14 @@
|
||||
console.log('[Poll] checkNotificationDelivery called');
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
console.log('[Poll] Status check result:', {
|
||||
console.log('[Poll] Status check result:', JSON.stringify({
|
||||
nextNotificationTime: result.nextNotificationTime,
|
||||
nextNotificationTimeDate: result.nextNotificationTime ? new Date(result.nextNotificationTime).toISOString() : null,
|
||||
lastNotificationTime: result.lastNotificationTime,
|
||||
lastKnownNextNotificationTime: lastKnownNextNotificationTime
|
||||
});
|
||||
lastNotificationTimeDate: result.lastNotificationTime ? new Date(result.lastNotificationTime).toISOString() : null,
|
||||
lastKnownNextNotificationTime: lastKnownNextNotificationTime,
|
||||
lastKnownNextNotificationTimeDate: lastKnownNextNotificationTime ? new Date(lastKnownNextNotificationTime).toISOString() : null
|
||||
}, null, 2));
|
||||
|
||||
// Check for notification delivery
|
||||
if (result.lastNotificationTime) {
|
||||
@@ -520,11 +590,13 @@
|
||||
|
||||
// Detect if nextNotificationTime changed (rollover occurred)
|
||||
const currentNextTime = result.nextNotificationTime;
|
||||
console.log('[Poll] Comparing nextNotificationTime:', {
|
||||
console.log('[Poll] Comparing nextNotificationTime:', JSON.stringify({
|
||||
current: currentNextTime,
|
||||
currentDate: currentNextTime ? new Date(currentNextTime).toISOString() : null,
|
||||
lastKnown: lastKnownNextNotificationTime,
|
||||
lastKnownDate: lastKnownNextNotificationTime ? new Date(lastKnownNextNotificationTime).toISOString() : null,
|
||||
changed: currentNextTime && currentNextTime !== lastKnownNextNotificationTime
|
||||
});
|
||||
}, null, 2));
|
||||
|
||||
if (currentNextTime && currentNextTime !== lastKnownNextNotificationTime) {
|
||||
if (lastKnownNextNotificationTime !== null) {
|
||||
@@ -558,11 +630,12 @@
|
||||
// Load plugin status automatically on page load
|
||||
window.addEventListener('load', () => {
|
||||
console.log('Page loaded, loading plugin status...');
|
||||
// Small delay to ensure Capacitor is ready
|
||||
setTimeout(() => {
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
// Small delay to ensure Capacitor is ready
|
||||
setTimeout(() => {
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
loadConfigurationStatus();
|
||||
|
||||
// Initialize last known next notification time
|
||||
if (window.DailyNotification) {
|
||||
@@ -580,6 +653,64 @@
|
||||
}, 500);
|
||||
});
|
||||
|
||||
// Refresh UI when app comes back to foreground (after force-stop, app resume, etc.)
|
||||
// This ensures the UI updates after recovery from force-stop
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (!document.hidden) {
|
||||
console.log('[Visibility] App became visible, refreshing UI status...');
|
||||
// Longer delay to allow recovery to complete (force-stop recovery can take a few seconds)
|
||||
// Also refresh immediately, then again after delay to catch any late recovery
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
loadConfigurationStatus();
|
||||
|
||||
setTimeout(() => {
|
||||
console.log('[Visibility] Delayed refresh after recovery period...');
|
||||
loadPluginStatus();
|
||||
loadPermissionStatus();
|
||||
loadChannelStatus();
|
||||
loadConfigurationStatus();
|
||||
|
||||
// Also check for recent notifications that might have been missed
|
||||
if (window.DailyNotification) {
|
||||
window.DailyNotification.getNotificationStatus()
|
||||
.then(result => {
|
||||
// Check if a notification was received recently (within last 2 minutes)
|
||||
if (result.lastNotificationTime) {
|
||||
const lastTime = new Date(result.lastNotificationTime);
|
||||
const now = new Date();
|
||||
const timeDiff = now - lastTime;
|
||||
|
||||
if (timeDiff > 0 && timeDiff < 120000) {
|
||||
console.log('[Visibility] Recent notification detected, showing indicator');
|
||||
const indicator = document.getElementById('notificationReceivedIndicator');
|
||||
const timeSpan = document.getElementById('notificationReceivedTime');
|
||||
|
||||
if (indicator && timeSpan) {
|
||||
indicator.style.display = 'block';
|
||||
lastTime.setSeconds(0, 0);
|
||||
timeSpan.textContent = `Received at ${lastTime.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true })}`;
|
||||
|
||||
// Hide after 30 seconds
|
||||
setTimeout(() => {
|
||||
indicator.style.display = 'none';
|
||||
}, 30000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update last known next notification time
|
||||
lastKnownNextNotificationTime = result.nextNotificationTime;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[Visibility] Failed to get notification status:', error);
|
||||
});
|
||||
}
|
||||
}, 3000); // Wait 3 seconds for recovery to complete (force-stop recovery can take time)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
console.log('Functions attached to window:', {
|
||||
|
||||
Reference in New Issue
Block a user