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

[firebase_messaging] check permissions #20

Closed
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
3 changes: 3 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.1.5
* Add checkIosNotificationSettings method to check whether the user has previously accepted or declined the push notifications on iOS devices.

## 5.1.4

* Update documentation to reflect new repository location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
NSNumber *value = call.arguments;
[FIRMessaging messaging].autoInitEnabled = value.boolValue;
result(nil);
} else if ([@"iosCheckPermissions" isEqualToString:method]) {
UIUserNotificationSettings *currentSettings =
[[UIApplication sharedApplication] currentUserNotificationSettings];
NSDictionary *settingsDictionary = @{
@"sound" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeSound],
@"badge" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeBadge],
@"alert" : [NSNumber numberWithBool:currentSettings.types & UIUserNotificationTypeAlert],
};
result(settingsDictionary);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class FirebaseMessaging {
throw UnsupportedError("Unrecognized JSON message");
}
}

Future<IosNotificationSettings> checkIosNotificationSettings() async {
return IosNotificationSettings._fromMap(
await _channel.invokeMapMethod<String, bool>('iosCheckPermissions'));
}
}

class IosNotificationSettings {
Expand All @@ -159,4 +164,14 @@ class IosNotificationSettings {

@override
String toString() => 'PushNotificationSettings ${toMap()}';

@override
int get hashCode => sound.hashCode ^ alert.hashCode ^ badge.hashCode;

@override
bool operator ==(dynamic other) =>
other is IosNotificationSettings &&
other.sound == sound &&
other.alert == alert &&
other.badge == badge;
}
6 changes: 3 additions & 3 deletions packages/firebase_messaging/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Cloud Messaging, a cross-platform
messaging solution that lets you reliably deliver messages on Android and iOS.
author: Flutter Team <[email protected]>
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging
version: 5.1.4
version: 5.1.5

flutter:
plugin:
Expand All @@ -27,5 +27,5 @@ dev_dependencies:
sdk: flutter

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
flutter: ">=1.5.0 <2.0.0"
sdk: '>=2.0.0-dev.28.0 <3.0.0'
flutter: '>=1.5.0 <2.0.0'
13 changes: 13 additions & 0 deletions packages/firebase_messaging/test/firebase_messaging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ void main() {

verify(mockChannel.invokeMethod<void>('setAutoInitEnabled', false));
});

test('checkIosNotificationSettings', () async {
when(mockChannel.invokeMapMethod<String, bool>('iosCheckPermissions'))
.thenAnswer((_) async =>
const IosNotificationSettings(sound: true, alert: true, badge: true)
.toMap());

expect(await firebaseMessaging.checkIosNotificationSettings(),
const IosNotificationSettings(sound: true, alert: true, badge: true));
verify(mockChannel.invokeMapMethod<String, bool>(captureAny))
.captured
.single;
});
}

class MockMethodChannel extends Mock implements MethodChannel {}