Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor -> Native iOS notifications #111

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.0.0-dev.2
* [iOS] Make native iOS notifications to remove `flutter_local_notification` dependency.

## 3.0.0-dev.1
**💥 Breaking Changes**
**🔧 Android installation steps were updated [here](https://github.com/gdelataillade/alarm/blob/main/help/INSTALL-ANDROID.md).**
Expand All @@ -10,7 +13,7 @@
* [Android] Move alarm service to native code.

## 2.1.1
* Fix AlarmSettings.fromJson method with missing [androidFullScreenIntent].
* Fix `AlarmSettings.fromJson` method with missing [androidFullScreenIntent].

## 2.1.0
**🔧 Android installation steps were updated [here](https://github.com/gdelataillade/alarm/blob/main/help/INSTALL-ANDROID.md).**
Expand Down
6 changes: 0 additions & 6 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ PODS:
- Flutter (1.0.0)
- flutter_fgbg (0.0.1):
- Flutter
- flutter_local_notifications (0.0.1):
- Flutter
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand All @@ -14,7 +12,6 @@ DEPENDENCIES:
- alarm (from `.symlinks/plugins/alarm/ios`)
- Flutter (from `Flutter`)
- flutter_fgbg (from `.symlinks/plugins/flutter_fgbg/ios`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

EXTERNAL SOURCES:
Expand All @@ -24,16 +21,13 @@ EXTERNAL SOURCES:
:path: Flutter
flutter_fgbg:
:path: ".symlinks/plugins/flutter_fgbg/ios"
flutter_local_notifications:
:path: ".symlinks/plugins/flutter_local_notifications/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
alarm: 6c1f6a9688f94cd6bf8f104c67cc26e78c9d8d13
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_fgbg: 31c0d1140a131daea2d342121808f6aa0dcd879d
flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
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.1')),
appBar: AppBar(title: const Text('alarm 3.0.0-dev.2')),
body: SafeArea(
child: alarms.isNotEmpty
? ListView.separated(
Expand Down
66 changes: 1 addition & 65 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0-dev.1"
args:
dependency: transitive
description:
name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
url: "https://pub.dev"
source: hosted
version: "2.4.2"
version: "3.0.0-dev.2"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -64,14 +56,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
dbus:
dependency: transitive
description:
name: dbus
sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac"
url: "https://pub.dev"
source: hosted
version: "0.7.10"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -117,30 +101,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.3"
flutter_local_notifications:
dependency: transitive
description:
name: flutter_local_notifications
sha256: "6d11ea777496061e583623aaf31923f93a9409ef8fcaeeefdd6cd78bf4fe5bb3"
url: "https://pub.dev"
source: hosted
version: "16.1.0"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
url: "https://pub.dev"
source: hosted
version: "4.0.0+1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "7cf643d6d5022f3baed0be777b0662cce5919c0a7b86e700299f22dc4ae660ef"
url: "https://pub.dev"
source: hosted
version: "7.0.0+1"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -215,14 +175,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.1"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: eeb2d1428ee7f4170e2bd498827296a18d4e7fc462b71727d111c0ac7707cfa6
url: "https://pub.dev"
source: hosted
version: "6.0.1"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -348,14 +300,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.1"
timezone:
dependency: transitive
description:
name: timezone
sha256: "1cfd8ddc2d1cfd836bc93e67b9be88c3adaeca6f40a00ca999104c30693cdca0"
url: "https://pub.dev"
source: hosted
version: "0.9.2"
vector_math:
dependency: transitive
description:
Expand Down Expand Up @@ -388,14 +332,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.3"
xml:
dependency: transitive
description:
name: xml
sha256: af5e77e9b83f2f4adc5d3f0a4ece1c7f45a2467b695c2540381bac793e34e556
url: "https://pub.dev"
source: hosted
version: "6.4.2"
sdks:
dart: ">=3.2.0 <4.0.0"
flutter: ">=3.16.0"
51 changes: 43 additions & 8 deletions ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,30 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {

let args = call.arguments as! Dictionary<String, Any>

let id = args["id"] as! Int
let delayInSeconds = args["delayInSeconds"] as! Double
let notificationTitle = args["notificationTitle"] as? String
let notificationBody = args["notificationBody"] as? String

if (notificationTitle != nil && notificationBody != nil && delayInSeconds >= 1.0) {
self.scheduleNotification(id: String(id), delayInSeconds: Int(floor(delayInSeconds)), title: notificationTitle!, body: notificationBody!)
}

notifOnKillEnabled = (args["notifOnKillEnabled"] as! Bool)
notificationTitleOnKill = (args["notifTitleOnAppKill"] as! String)
notificationBodyOnKill = (args["notifDescriptionOnAppKill"] as! String)

if notifOnKillEnabled && !observerAdded {
observerAdded = true
do {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate(_:)), name: UIApplication.willTerminateNotification, object: nil)
} catch {
NSLog("SwiftAlarmPlugin: Failed to register observer for UIApplication.willTerminateNotification: \(error)")
}
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate(_:)), name: UIApplication.willTerminateNotification, object: nil)
}

let id = args["id"] as! Int
let delayInSeconds = args["delayInSeconds"] as! Double
let loopAudio = args["loopAudio"] as! Bool
let fadeDuration = args["fadeDuration"] as! Double
let vibrationsEnabled = args["vibrate"] as! Bool
let volume = args["volume"] as? Double
let assetAudio = args["assetAudio"] as! String

var volumeFloat: Float? = nil
if let volumeValue = volume {
volumeFloat = Float(volumeValue)
Expand Down Expand Up @@ -249,6 +252,8 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}

private func stopAlarm(id: Int, result: FlutterResult) {
self.cancelNotification(id: String(id))

self.mixOtherAudios()

self.vibrate = false
Expand Down Expand Up @@ -421,4 +426,34 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
NSLog("SwiftAlarmPlugin: BGTaskScheduler not available for your version of iOS lower than 13.0")
}
}

func scheduleNotification(id: String, delayInSeconds: Int, title: String, body: String) {
// Request permission
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
// Schedule the notification
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(delayInSeconds), repeats: false)
let request = UNNotificationRequest(identifier: "alarm-\(id)", content: content, trigger: trigger)

center.add(request) { error in
if let error = error {
NSLog("SwiftAlarmPlugin: Error scheduling notification: \(error.localizedDescription)")
}
}
} else {
NSLog("SwiftAlarmPlugin: Notification permission denied")
}
}
}

func cancelNotification(id: String) {
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: ["alarm-\(id)"])
}
}
18 changes: 0 additions & 18 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:async';
import 'package:alarm/model/alarm_settings.dart';
import 'package:alarm/src/ios_alarm.dart';
import 'package:alarm/src/android_alarm.dart';
import 'package:alarm/service/notification.dart';
import 'package:alarm/service/storage.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -38,7 +37,6 @@ class Alarm {

await Future.wait([
if (android) AndroidAlarm.init(),
AlarmNotification.instance.init(),
AlarmStorage.init(),
]);
await checkAlarm();
Expand Down Expand Up @@ -82,20 +80,6 @@ class Alarm {

await AlarmStorage.saveAlarm(alarmSettings);

if (iOS) {
await AlarmNotification.instance.scheduleAlarmNotif(
id: alarmSettings.id,
dateTime: alarmSettings.dateTime,
title: alarmSettings.notificationTitle,
body: alarmSettings.notificationBody,
fullScreenIntent: alarmSettings.androidFullScreenIntent,
);
}

if (alarmSettings.enableNotificationOnKill) {
await AlarmNotification.instance.requestNotificationPermission();
}

if (iOS) {
return IOSAlarm.setAlarm(
alarmSettings,
Expand Down Expand Up @@ -129,8 +113,6 @@ class Alarm {
static Future<bool> stop(int id) async {
await AlarmStorage.unsaveAlarm(id);

AlarmNotification.instance.cancel(id);

return iOS ? await IOSAlarm.stopAlarm(id) : await AndroidAlarm.stop(id);
}

Expand Down
Loading