Skip to content

Commit e6ea52f

Browse files
committed
Add method to check current iOS notification settings
1 parent cdbcf01 commit e6ea52f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

packages/firebase_messaging/ios/Classes/FirebaseMessagingPlugin.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
122122
NSNumber *value = call.arguments;
123123
[FIRMessaging messaging].autoInitEnabled = value.boolValue;
124124
result(nil);
125+
} else if ([@"iosCheckPermissions" isEqualToString:method]) {
126+
UIUserNotificationSettings *currentSettings =
127+
[[UIApplication sharedApplication] currentUserNotificationSettings];
128+
NSDictionary *settingsDictionary = @{
129+
@"sound" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeSound],
130+
@"badge" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeBadge],
131+
@"alert" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeAlert],
132+
};
133+
result(settingsDictionary);
125134
} else {
126135
result(FlutterMethodNotImplemented);
127136
}

packages/firebase_messaging/lib/firebase_messaging.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ class FirebaseMessaging {
134134
throw UnsupportedError("Unrecognized JSON message");
135135
}
136136
}
137+
138+
Future<IosNotificationSettings> checkIosNotificationSettings() async {
139+
return IosNotificationSettings._fromMap(
140+
await _channel.invokeMapMethod<String, bool>('iosCheckPermissions'));
141+
}
137142
}
138143

139144
class IosNotificationSettings {
@@ -159,4 +164,14 @@ class IosNotificationSettings {
159164

160165
@override
161166
String toString() => 'PushNotificationSettings ${toMap()}';
167+
168+
@override
169+
int get hashCode => sound.hashCode ^ alert.hashCode ^ badge.hashCode;
170+
171+
@override
172+
bool operator ==(dynamic other) =>
173+
other is IosNotificationSettings &&
174+
other.sound == sound &&
175+
other.alert == alert &&
176+
other.badge == badge;
162177
}

packages/firebase_messaging/test/firebase_messaging_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ void main() {
166166

167167
verify(mockChannel.invokeMethod<void>('setAutoInitEnabled', false));
168168
});
169+
170+
test('checkIosNotificationSettings', () async {
171+
when(mockChannel.invokeMapMethod<String, bool>('iosCheckPermissions'))
172+
.thenAnswer((_) async =>
173+
const IosNotificationSettings(sound: true, alert: true, badge: true)
174+
.toMap());
175+
176+
expect(await firebaseMessaging.checkIosNotificationSettings(),
177+
const IosNotificationSettings(sound: true, alert: true, badge: true));
178+
verify(mockChannel.invokeMapMethod<String, bool>(captureAny))
179+
.captured
180+
.single;
181+
});
169182
}
170183

171184
class MockMethodChannel extends Mock implements MethodChannel {}

0 commit comments

Comments
 (0)