Skip to content

Commit

Permalink
Don't attempt to invoke 'FcmDartService#start' for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmartineau committed Mar 16, 2020
1 parent 046c4d1 commit 2f7f200
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.0.13

* Fix error invoking `configure` with `onBackgroundMessage` on iOS.

## 6.0.12

* Replace deprecated `getFlutterEngine` call on Android.
Expand Down
29 changes: 19 additions & 10 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,23 @@ class FirebaseMessaging {
}

/// Sets up [MessageHandler] for incoming messages.
void configure({
///
/// @param onBackgroundMessage This callback must be a top-level or static function, otherwise an [ArgumentError] will
/// be raised
///
/// @returns true if a background handler was configured, false otherwise
/// @throws ArgumentError is onBackgroundMessage is invalid
Future<bool> configure({
MessageHandler onMessage,
MessageHandler onBackgroundMessage,
MessageHandler onLaunch,
MessageHandler onResume,
}) {
}) async {
_onMessage = onMessage;
_onLaunch = onLaunch;
_onResume = onResume;
_channel.setMethodCallHandler(_handleMethod);
_channel.invokeMethod<void>('configure');
await _channel.invokeMethod<void>('configure');
if (onBackgroundMessage != null) {
_onBackgroundMessage = onBackgroundMessage;
final CallbackHandle backgroundSetupHandle =
Expand All @@ -127,14 +133,17 @@ class FirebaseMessaging {
);
}

_channel.invokeMethod<bool>(
'FcmDartService#start',
<String, dynamic>{
'setupHandle': backgroundSetupHandle.toRawHandle(),
'backgroundHandle': backgroundMessageHandle.toRawHandle()
},
);
if (_platform.isAndroid) {
return await _channel.invokeMethod<bool>(
'FcmDartService#start',
<String, dynamic>{
'setupHandle': backgroundSetupHandle.toRawHandle(),
'backgroundHandle': backgroundMessageHandle.toRawHandle()
},
);
}
}
return false;
}

final StreamController<String> _tokenStreamController =
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_messaging/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: firebase_messaging
description: Flutter plugin for Firebase Cloud Messaging, a cross-platform
messaging solution that lets you reliably deliver messages on Android and iOS.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging
version: 6.0.12
version: 6.0.13

flutter:
plugin:
Expand Down
43 changes: 40 additions & 3 deletions packages/firebase_messaging/test/firebase_messaging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart' show TestWidgetsFlutterBinding;
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:test/test.dart';

const fcmDartServiceStart = 'FcmDartService#start';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

Expand All @@ -19,6 +21,8 @@ void main() {

setUp(() {
mockChannel = MockMethodChannel();
when(mockChannel.invokeMethod<bool>(fcmDartServiceStart, any))
.thenAnswer((_) => Future.value(true));
firebaseMessaging = FirebaseMessaging.private(
mockChannel, FakePlatform(operatingSystem: 'ios'));
});
Expand Down Expand Up @@ -182,13 +186,46 @@ void main() {
verify(mockChannel.invokeMethod<void>('setAutoInitEnabled', false));
});

test('configure bad onBackgroundMessage', () {
test('configure bad onBackgroundMessage iOS', () async {
expect(
firebaseMessaging.configure(
onBackgroundMessage: (dynamic message) => Future<dynamic>.value(),
),
throwsArgumentError,
);
// Even with the bad arg, configure still should have been called
verify(mockChannel.invokeMethod("configure"));
});

test('configure bad onBackgroundMessage android', () async {
final firebaseMessaging = FirebaseMessaging.private(
mockChannel, FakePlatform(operatingSystem: 'android'));
expect(
() => firebaseMessaging.configure(
firebaseMessaging.configure(
onBackgroundMessage: (dynamic message) => Future<dynamic>.value(),
),
throwsArgumentError,
);
// Even though there was an error, configure still should have been called.
verify(mockChannel.invokeMethod("configure"));
});

test('configure onBackgroundMessage android', () async {
final firebaseMessaging = FirebaseMessaging.private(
mockChannel, FakePlatform(operatingSystem: 'android'));
final result = await firebaseMessaging.configure(
onBackgroundMessage: validOnBackgroundMessage);
expect(result, isTrue);
verify(mockChannel.invokeMethod<void>("configure"));
verify(mockChannel.invokeMethod<bool>(fcmDartServiceStart, any));
});

test('configure onBackgroundMessage ios', () async {
final result = await firebaseMessaging.configure(
onBackgroundMessage: validOnBackgroundMessage);
expect(result, isFalse);
verify(mockChannel.invokeMethod("configure"));
verifyNever(mockChannel.invokeMethod(fcmDartServiceStart, any));
});
}

Expand Down

0 comments on commit 2f7f200

Please sign in to comment.