Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add error message for passing a bad background message
  • Loading branch information
bparrishMines committed Oct 2, 2019
commit 4ba4ccae32df94e19767698c0ad2aff43425e772
9 changes: 9 additions & 0 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ class FirebaseMessaging {
PluginUtilities.getCallbackHandle(_fcmSetupBackgroundChannel);
final CallbackHandle backgroundMessageHandle =
PluginUtilities.getCallbackHandle(_onBackgroundMessage);

if (backgroundMessageHandle == null) {
throw ArgumentError(
'''Failed to setup background message handler! `onBackgroundMessage`
should be a TOP-LEVEL OR STATIC FUNCTION and should NOT be tied to a
class or an anonymous function.''',
);
}

_channel.invokeMethod<bool>(
'FcmDartService#start',
<String, dynamic>{
Expand Down
30 changes: 23 additions & 7 deletions packages/firebase_messaging/test/firebase_messaging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ void main() {
final Completer<dynamic> onLaunch = Completer<dynamic>();
final Completer<dynamic> onResume = Completer<dynamic>();

firebaseMessaging.configure(onMessage: (dynamic m) async {
onMessage.complete(m);
}, onLaunch: (dynamic m) async {
onLaunch.complete(m);
}, onResume: (dynamic m) async {
onResume.complete(m);
});
firebaseMessaging.configure(
onMessage: (dynamic m) async {
onMessage.complete(m);
},
onLaunch: (dynamic m) async {
onLaunch.complete(m);
},
onResume: (dynamic m) async {
onResume.complete(m);
},
onBackgroundMessage: validOnBackgroundMessage,
);
final dynamic handler =
verify(mockChannel.setMethodCallHandler(captureAny)).captured.single;

Expand Down Expand Up @@ -166,6 +171,17 @@ void main() {

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

test('configure bad onBackgroundMessage', () {
expect(
() => firebaseMessaging.configure(
onBackgroundMessage: (dynamic message) => Future<dynamic>.value(),
),
throwsArgumentError,
);
});
}

Future<dynamic> validOnBackgroundMessage(Map<String, dynamic> message) async {}

class MockMethodChannel extends Mock implements MethodChannel {}