Skip to content

Commit

Permalink
Merge branch 'master' into @russell/bump-sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Nov 3, 2021
2 parents ee268ae + 3415ab1 commit 5f003e9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/messaging/usage.mdx
Expand Up @@ -94,7 +94,7 @@ The `authorizationStatus` property can return a value which can be used to deter
- `notDetermined`: The user has not yet chosen whether to grant permission.
- `provisional`: The user granted provisional permission (see [Provisional Permission](permissions#provisional-permission)).

> On Android `authorizationStatus` is always `authorized`.
> On Android `authorizationStatus` will return `authorized` if the user has not disabled notifications for the app via the operating systems settings.
The other properties on `NotificationSettings` return whether a specific permission is enabled, disabled or not supported on the current
device.
Expand Down
Expand Up @@ -13,6 +13,7 @@
import android.content.IntentFilter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
Expand Down Expand Up @@ -250,6 +251,18 @@ private Task<Map<String, Object>> getInitialMessage(Map<String, Object> argument
});
}

private Task<Map<String, Integer>> getPermissions() {
return Tasks.call(
cachedThreadPool,
() -> {
final Map<String, Integer> permissions = new HashMap<>();
final boolean areNotificationsEnabled =
NotificationManagerCompat.from(mainActivity).areNotificationsEnabled();
permissions.put("authorizationStatus", areNotificationsEnabled ? 1 : 0);
return permissions;
});
}

@Override
public void onMethodCall(final MethodCall call, @NonNull final Result result) {
Task<?> methodCallTask;
Expand Down Expand Up @@ -319,6 +332,10 @@ public void onMethodCall(final MethodCall call, @NonNull final Result result) {
case "Messaging#setAutoInitEnabled":
methodCallTask = setAutoInitEnabled(call.arguments());
break;
case "Messaging#requestPermission":
case "Messaging#getNotificationSettings":
methodCallTask = getPermissions();
break;
default:
result.notImplemented();
return;
Expand Down
Expand Up @@ -34,6 +34,21 @@ class _Permissions extends State<Permissions> {
});
}

Future<void> checkPermissions() async {
setState(() {
_fetching = true;
});

NotificationSettings settings =
await FirebaseMessaging.instance.getNotificationSettings();

setState(() {
_requested = true;
_fetching = false;
_settings = settings;
});
}

Widget row(String title, String value) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
Expand Down Expand Up @@ -72,7 +87,7 @@ class _Permissions extends State<Permissions> {
row('Sound', settingsMap[_settings.sound]!),
],
ElevatedButton(
onPressed: () => {}, child: const Text('Reload Permissions')),
onPressed: checkPermissions, child: const Text('Reload Permissions')),
]);
}
}
Expand Down
Expand Up @@ -137,10 +137,9 @@ class FirebaseMessaging extends FirebasePluginPlatform {
///
/// - On iOS, a dialog is shown requesting the users permission.
/// - On macOS, a notification will appear asking to grant permission.
/// - On Android, is it not required to call this method. If called however,
/// a [NotificationSettings] class will be returned with
/// [NotificationSettings.authorizationStatus] returning
/// [AuthorizationStatus.authorized].
/// - On Android, a [NotificationSettings] class will be returned with the
/// value of [NotificationSettings.authorizationStatus] indicating whether
/// the app has notifications enabled or blocked in the system settings.
/// - On Web, a popup requesting the users permission is shown using the native browser API.
///
/// Note that on iOS, if [provisional] is set to `true`, silent notification permissions will be
Expand Down
Expand Up @@ -132,7 +132,7 @@ void main() {
criticalAlert: anyNamed('criticalAlert'),
provisional: anyNamed('provisional'),
sound: anyNamed('sound'),
)).thenAnswer((_) => Future.value(androidNotificationSettings));
)).thenAnswer((_) => Future.value(defaultNotificationSettings));

// true values
await messaging!.requestPermission(
Expand Down
Expand Up @@ -233,8 +233,9 @@ class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {
@override
Future<NotificationSettings> getNotificationSettings() async {
if (defaultTargetPlatform != TargetPlatform.iOS &&
defaultTargetPlatform != TargetPlatform.macOS) {
return androidNotificationSettings;
defaultTargetPlatform != TargetPlatform.macOS &&
defaultTargetPlatform != TargetPlatform.android) {
return defaultNotificationSettings;
}

try {
Expand All @@ -250,17 +251,19 @@ class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {
}

@override
Future<NotificationSettings> requestPermission(
{bool alert = true,
bool announcement = false,
bool badge = true,
bool carPlay = false,
bool criticalAlert = false,
bool provisional = false,
bool sound = true}) async {
Future<NotificationSettings> requestPermission({
bool alert = true,
bool announcement = false,
bool badge = true,
bool carPlay = false,
bool criticalAlert = false,
bool provisional = false,
bool sound = true,
}) async {
if (defaultTargetPlatform != TargetPlatform.iOS &&
defaultTargetPlatform != TargetPlatform.macOS) {
return androidNotificationSettings;
defaultTargetPlatform != TargetPlatform.macOS &&
defaultTargetPlatform != TargetPlatform.android) {
return defaultNotificationSettings;
}

try {
Expand Down
Expand Up @@ -110,8 +110,8 @@ NotificationSettings convertToNotificationSettings(Map<String, int> map) {
);
}

/// Used to return [NotificationSettings] for all Android devices.
const NotificationSettings androidNotificationSettings = NotificationSettings(
// Default [NotificationSettings] for platforms which do not require permissions
const NotificationSettings defaultNotificationSettings = NotificationSettings(
authorizationStatus: AuthorizationStatus.authorized,
alert: AppleNotificationSetting.notSupported,
announcement: AppleNotificationSetting.notSupported,
Expand Down

0 comments on commit 5f003e9

Please sign in to comment.