fix(android): stop auto-opening Settings for exact alarm in scheduleDailyNotification

Remove the guard that opened system Settings and rejected when exact alarms
were not granted. Scheduling now proceeds using inexact/windowed fallback;
consuming apps can handle UX (e.g. optional hint or openExactAlarmSettings()).
This commit is contained in:
Jose Olarte III
2026-03-09 20:29:04 +08:00
parent daaf7aa62a
commit 6df1d4a7c6

View File

@@ -1055,51 +1055,14 @@ open class DailyNotificationPlugin : Plugin() {
return call.reject("Context not available") return call.reject("Context not available")
} }
// Check if exact alarms can be scheduled // Do not open Settings or reject when exact alarms are not granted.
// Proceed with scheduling; underlying layer uses inexact/windowed alarms when exact is unavailable.
// Apps that want to prompt for exact alarm can use openExactAlarmSettings() or requestExactAlarmPermission().
if (!canScheduleExactAlarms(context)) { if (!canScheduleExactAlarms(context)) {
// Permission not granted - request it Log.i(TAG, "Exact alarm permission not granted; scheduling will use inexact/windowed fallback.")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && canRequestExactAlarmPermission(context)) {
// Open Settings to let user grant permission
try {
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
data = android.net.Uri.parse("package:${context.packageName}")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
Log.w(TAG, "Exact alarm permission required. Opened Settings for user to grant permission.")
call.reject(
"Exact alarm permission required. Please grant 'Alarms & reminders' permission in Settings, then try again.",
"EXACT_ALARM_PERMISSION_REQUIRED"
)
return
} catch (e: Exception) {
Log.e(TAG, "Failed to open exact alarm settings", e)
call.reject("Failed to open exact alarm settings: ${e.message}")
return
}
} else {
// Permission permanently denied - direct to app settings
try {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = android.net.Uri.parse("package:${context.packageName}")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
Log.w(TAG, "Exact alarm permission denied. Directing user to app settings.")
call.reject(
"Exact alarm permission denied. Please enable 'Alarms & reminders' in app settings.",
"PERMISSION_DENIED"
)
return
} catch (e: Exception) {
Log.e(TAG, "Failed to open app settings", e)
call.reject("Failed to open app settings: ${e.message}")
return
}
}
} }
// Permission granted - proceed with exact alarm scheduling // Proceed with scheduling (exact when granted, otherwise inexact/windowed)
// Capacitor passes the object directly via call.data // Capacitor passes the object directly via call.data
val options = call.data ?: return call.reject("Options are required") val options = call.data ?: return call.reject("Options are required")