Skip to content

Commit

Permalink
Add alarm fixes for version 3.0.0-dev.5
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Dec 2, 2023
1 parent 59bd3ff commit 1b7bd5a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 23 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0-dev.5
* Improve README and installation guides.
* [iOS] Add more error handling.
* [Android] Fix alarm for Android 12 and above.

## 3.0.0-dev.4
* [iOS] Remove notification sound.
* Throw exception if alarm settings are invalid.
Expand All @@ -8,7 +13,7 @@
* Add minor improvements.

## 3.0.0-dev.2
* [iOS] Make native iOS notifications to remove `flutter_local_notification` dependency.
* [iOS] Make native iOS notifications to remove `flutter_local_notifications` dependency.

## 3.0.0-dev.1
**💥 Breaking Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AlarmService : Service() {
val notificationHandler = NotificationHandler(this)

val intent = applicationContext.packageManager.getLaunchIntentForPackage(applicationContext.packageName)
val pendingIntent = PendingIntent.getActivity(this, id!!, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent = PendingIntent.getActivity(this, id!!, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

val notification = notificationHandler.buildNotification(notificationTitle!!, notificationBody!!, fullScreenIntent!!, pendingIntent)
startForeground(id, notification)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class _ExampleAlarmHomeScreenState extends State<ExampleAlarmHomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('alarm 3.0.0-dev.4')),
appBar: AppBar(title: const Text('alarm 3.0.0-dev.5')),
body: SafeArea(
child: alarms.isNotEmpty
? ListView.separated(
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0-dev.4"
version: "3.0.0-dev.5"
async:
dependency: transitive
description:
Expand Down
6 changes: 4 additions & 2 deletions help/INSTALL-ANDROID.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ Inside the <application> tag of your AndroidManifest.xml, add the following decl

This setup is essential for managing notifications, especially when the app is terminated or the device is rebooted.

## Additional Resource
## Additional Resources

For a practical implementation example, you can refer to the example's Android manifest & build.gradle in the plugin repository. This might help you better understand the setup and integration:

[Example build.gradle](https://github.com/gdelataillade/alarm/blob/main/example/android/app/build.gradle)
[Example AndroidManifest.xml](https://github.com/gdelataillade/alarm/blob/main/example/android/app/src/main/AndroidManifest.xml)
[Example AndroidManifest.xml](https://github.com/gdelataillade/alarm/blob/main/example/android/app/src/main/AndroidManifest.xml)

Note that in version `3.0.0`, I removed the [`flutter_local_notifications`](https://pub.dev/packages/flutter_local_notifications) and [`android_alarm_manager_plus`](https://pub.dev/packages/android_alarm_manager_plus) dependencies. For those upgrading from versions earlier than `3.0.0`, please ensure to remove any configuration steps related to these dependencies.
42 changes: 26 additions & 16 deletions ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
private func setAlarm(call: FlutterMethodCall, result: FlutterResult) {
self.mixOtherAudios()

let args = call.arguments as! Dictionary<String, Any>
guard let args = call.arguments as? [String: Any] else {
result(FlutterError(code: "NATIVE_ERR", message: "[Alarm] Arguments are not in the expected format", details: nil))
return
}

let id = args["id"] as! Int
let delayInSeconds = args["delayInSeconds"] as! Double
Expand Down Expand Up @@ -98,17 +101,17 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
if assetAudio.hasPrefix("assets/") {
let filename = registrar.lookupKey(forAsset: assetAudio)

if let audioPath = Bundle.main.path(forResource: filename, ofType: nil) {
guard let audioPath = Bundle.main.path(forResource: filename, ofType: nil) else {
result(FlutterError(code: "NATIVE_ERR", message: "[Alarm] Audio file not found: \(assetAudio)", details: nil))
return
}

do {
let audioUrl = URL(fileURLWithPath: audioPath)
do {
let audioPlayer = try AVAudioPlayer(contentsOf: audioUrl)
self.audioPlayers[id] = audioPlayer
} catch {
result(FlutterError.init(code: "NATIVE_ERR", message: "[Alarm] Error loading AVAudioPlayer with given Flutter asset path: \(assetAudio)", details: nil))
return
}
} else {
result(FlutterError.init(code: "NATIVE_ERR", message: "[Alarm] Error finding audio file: \(assetAudio)", details: nil))
let audioPlayer = try AVAudioPlayer(contentsOf: audioUrl)
self.audioPlayers[id] = audioPlayer
} catch {
result(FlutterError(code: "NATIVE_ERR", message: "[Alarm] Error loading audio player: \(error.localizedDescription)", details: nil))
return
}
} else {
Expand All @@ -125,27 +128,32 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}
}

let currentTime = self.audioPlayers[id]!.deviceCurrentTime
guard let audioPlayer = self.audioPlayers[id] else {
result(FlutterError(code: "NATIVE_ERR", message: "[Alarm] Audio player not found for ID: \(id)", details: nil))
return
}

let currentTime = audioPlayer.deviceCurrentTime
let time = currentTime + delayInSeconds

let dateTime = Date().addingTimeInterval(delayInSeconds)
self.triggerTimes[id] = dateTime

if loopAudio {
self.audioPlayers[id]!.numberOfLoops = -1
audioPlayer.numberOfLoops = -1
}

self.audioPlayers[id]!.prepareToPlay()
audioPlayer.prepareToPlay()

if fadeDuration > 0.0 {
self.audioPlayers[id]!.volume = 0.1
audioPlayer.volume = 0.1
}

if !playSilent {
self.startSilentSound()
}

self.audioPlayers[id]!.play(atTime: time)
audioPlayer.play(atTime: time)

self.tasksQueue[id] = DispatchWorkItem(block: {
self.handleAlarmAfterDelay(
Expand Down Expand Up @@ -243,6 +251,8 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}
}

NSLog("SwiftAlarmPlugin: fadeDuration is \(fadeDuration)s and volume is \(String(describing: volume))%");

if let volumeValue = volume {
self.setVolume(volume: volumeValue, enable: true)
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: alarm
description: A simple Flutter alarm manager plugin for both iOS and Android.
version: 3.0.0-dev.4
version: 3.0.0-dev.5
homepage: https://github.com/gdelataillade/alarm

environment:
Expand Down

0 comments on commit 1b7bd5a

Please sign in to comment.