Skip to content

Commit

Permalink
Add notification settings getter
Browse files Browse the repository at this point in the history
  • Loading branch information
hgadalrab committed Oct 4, 2024
1 parent 8fb1100 commit 68cc7fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
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

0 comments on commit 68cc7fa

Please sign in to comment.