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

Add notification settings getter #9

Merged
merged 1 commit into from
Oct 4, 2024
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
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ packages:
path: "../flutter_apns"
relative: true
source: path
version: "1.7.3"
version: "1.7.5"
flutter_apns_only:
dependency: transitive
description:
path: "../flutter_apns_only"
relative: true
source: path
version: "1.7.3"
version: "1.7.5"
flutter_local_notifications:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion flutter_apns/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ packages:
path: "../flutter_apns_only"
relative: true
source: path
version: "1.7.3"
version: "1.7.5"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion flutter_apns/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_apns
description: APNS push notification plugin. Uses firebase_messaging on Android, but replaces it on iOS with custom implementation.
version: 1.7.3
version: 1.7.5
homepage: https://github.com/mwaylabs/flutter-apns

publish_to: 'none'
Expand Down
42 changes: 24 additions & 18 deletions flutter_apns_only/ios/Classes/FlutterApnsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func getFlutterError(_ error: Error) -> FlutterError {
case "setNotificationCategories":
setNotificationCategories(arguments: call.arguments!)
result(nil)
case "getNotificationSettings":
getNotificationSettings(){(value) in
result(value)
}
default:
assertionFailure(call.method)
result(FlutterMethodNotImplemented)
Expand Down Expand Up @@ -105,6 +109,23 @@ func getFlutterError(_ error: Error) -> FlutterError {
}
}

func getNotificationSettings(completion: @escaping (([String: Bool]) -> Void)){
let center = UNUserNotificationCenter.current()
let application = UIApplication.shared

assert(center.delegate != nil)

center.getNotificationSettings { (settings) in
let map = [
"sound": settings.soundSetting == .enabled,
"badge": settings.badgeSetting == .enabled,
"alert": settings.alertSetting == .enabled,
"carPlay": settings.carPlaySetting == .enabled,
]
completion(map)
}
}

func requestNotificationPermissions(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let center = UNUserNotificationCenter.current()
let application = UIApplication.shared
Expand All @@ -130,14 +151,6 @@ func getFlutterError(_ error: Error) -> FlutterError {
if readBool("carPlay") {
options.append(.carPlay)
}

var provisionalRequested = false
if #available(iOS 12.0, *) {
if readBool("provisional") {
options.append(.provisional)
provisionalRequested = true
}
}


let optionsUnion = UNAuthorizationOptions(options)
Expand All @@ -148,16 +161,9 @@ func getFlutterError(_ error: Error) -> FlutterError {
return
}

center.getNotificationSettings { (settings) in
let map = [
"sound": settings.soundSetting == .enabled,
"badge": settings.badgeSetting == .enabled,
"alert": settings.alertSetting == .enabled,
"carPlay": settings.carPlaySetting == .enabled,
"provisional": granted && provisionalRequested
]

self.channel.invokeMethod("onIosSettingsRegistered", arguments: map)
self.getNotificationSettings(){(value) in
self.channel.invokeMethod("onIosSettingsRegistered", arguments: value)

}

result(granted)
Expand Down
10 changes: 8 additions & 2 deletions flutter_apns_only/lib/flutter_apns_only.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class ApnsPushConnectorOnly {
return null;
case 'onIosSettingsRegistered':
final obj = IosNotificationSettings._fromMap(call.arguments.cast<String, bool>());

_iosSettingsStreamController.add(obj);
isDisabledByUser.value = obj.alert == false;
return null;
return;
case 'onMessage':
return _onMessage?.call(_extractMessage(call));
case 'onLaunch':
Expand Down Expand Up @@ -128,6 +128,12 @@ class ApnsPushConnectorOnly {
);
}

/// https://developer.apple.com/documentation/usernotifications/unnotificationsettings/
Future<IosNotificationSettings> getNotificationSettings() async {
final result = await _channel.invokeMethod<Map<String, dynamic>>('getNotificationSettings');
return IosNotificationSettings._fromMap(result as Map<String, bool>);
}

Future<void> unregister() async {
await _channel.invokeMethod('unregister');
token.value = null;
Expand Down
2 changes: 1 addition & 1 deletion flutter_apns_only/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_apns_only
description: APNS push notification plugin. Works only on iOS. See flutter_apns for apns & firebase combo
version: 1.7.3
version: 1.7.5
homepage: https://github.com/mwaylabs/flutter-apns

environment:
Expand Down