Skip to content

Commit

Permalink
[firebase_messaging] Add an ArgumentError when passing invalid backgr…
Browse files Browse the repository at this point in the history
…ound message (firebase#252)
  • Loading branch information
bparrishMines authored Nov 21, 2019
1 parent ad5ab7a commit 14ef4ea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
7 changes: 6 additions & 1 deletion packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.0.1

* `FirebaseMessaging.configure` will throw an `ArgumentError` when `onBackgroundMessage` parameter
is not a top-level or static function.

## 6.0.0

* Use `UNUserNotificationCenter` to receive messages on iOS version >= 10.
Expand All @@ -11,7 +16,7 @@

and add this line to your iOS project `AppDelegate.m`

```objectivec
```swift
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/firebase_messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ for more.

By default background messaging is not enabled. To handle messages in the background:

1. Add an Application.java class to your app
1. Add an Application.java class to your app in the same directory as your `MainActivity.java`. This is typically found in `<app-name>/android/app/src/main/java/<app-organization-path>/`.

```
package io.flutter.plugins.firebasemessagingexample;
Expand All @@ -85,11 +85,11 @@ By default background messaging is not enabled. To handle messages in the backgr
}
}
```
1. Set name property of application in `AndroidManifest.xml`
1. Set name property of application in `AndroidManifest.xml`. This is typically found in `<app-name>/android/app/src/main/`.
```
<application android:name=".Application" ...>
```
1. Define a top level Dart method to handle background messages
1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages
```
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
Expand Down
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 @@ -118,6 +118,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
2 changes: 1 addition & 1 deletion 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: 6.0.0
version: 6.0.1

flutter:
plugin:
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 {}

0 comments on commit 14ef4ea

Please sign in to comment.