Skip to content

Commit 14ef4ea

Browse files
[firebase_messaging] Add an ArgumentError when passing invalid background message (#252)
1 parent ad5ab7a commit 14ef4ea

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

packages/firebase_messaging/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.0.1
2+
3+
* `FirebaseMessaging.configure` will throw an `ArgumentError` when `onBackgroundMessage` parameter
4+
is not a top-level or static function.
5+
16
## 6.0.0
27

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

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

14-
```objectivec
19+
```swift
1520
if (@available(iOS 10.0, *)) {
1621
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
1722
}

packages/firebase_messaging/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ for more.
6161

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

64-
1. Add an Application.java class to your app
64+
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>/`.
6565

6666
```
6767
package io.flutter.plugins.firebasemessagingexample;
@@ -85,11 +85,11 @@ By default background messaging is not enabled. To handle messages in the backgr
8585
}
8686
}
8787
```
88-
1. Set name property of application in `AndroidManifest.xml`
88+
1. Set name property of application in `AndroidManifest.xml`. This is typically found in `<app-name>/android/app/src/main/`.
8989
```
9090
<application android:name=".Application" ...>
9191
```
92-
1. Define a top level Dart method to handle background messages
92+
1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages
9393
```
9494
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
9595
if (message.containsKey('data')) {

packages/firebase_messaging/lib/firebase_messaging.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ class FirebaseMessaging {
118118
PluginUtilities.getCallbackHandle(_fcmSetupBackgroundChannel);
119119
final CallbackHandle backgroundMessageHandle =
120120
PluginUtilities.getCallbackHandle(_onBackgroundMessage);
121+
122+
if (backgroundMessageHandle == null) {
123+
throw ArgumentError(
124+
'''Failed to setup background message handler! `onBackgroundMessage`
125+
should be a TOP-LEVEL OR STATIC FUNCTION and should NOT be tied to a
126+
class or an anonymous function.''',
127+
);
128+
}
129+
121130
_channel.invokeMethod<bool>(
122131
'FcmDartService#start',
123132
<String, dynamic>{

packages/firebase_messaging/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Cloud Messaging, a cross-platform
33
messaging solution that lets you reliably deliver messages on Android and iOS.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging
6-
version: 6.0.0
6+
version: 6.0.1
77

88
flutter:
99
plugin:

packages/firebase_messaging/test/firebase_messaging_test.dart

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,18 @@ void main() {
9797
final Completer<dynamic> onLaunch = Completer<dynamic>();
9898
final Completer<dynamic> onResume = Completer<dynamic>();
9999

100-
firebaseMessaging.configure(onMessage: (dynamic m) async {
101-
onMessage.complete(m);
102-
}, onLaunch: (dynamic m) async {
103-
onLaunch.complete(m);
104-
}, onResume: (dynamic m) async {
105-
onResume.complete(m);
106-
});
100+
firebaseMessaging.configure(
101+
onMessage: (dynamic m) async {
102+
onMessage.complete(m);
103+
},
104+
onLaunch: (dynamic m) async {
105+
onLaunch.complete(m);
106+
},
107+
onResume: (dynamic m) async {
108+
onResume.complete(m);
109+
},
110+
onBackgroundMessage: validOnBackgroundMessage,
111+
);
107112
final dynamic handler =
108113
verify(mockChannel.setMethodCallHandler(captureAny)).captured.single;
109114

@@ -166,6 +171,17 @@ void main() {
166171

167172
verify(mockChannel.invokeMethod<void>('setAutoInitEnabled', false));
168173
});
174+
175+
test('configure bad onBackgroundMessage', () {
176+
expect(
177+
() => firebaseMessaging.configure(
178+
onBackgroundMessage: (dynamic message) => Future<dynamic>.value(),
179+
),
180+
throwsArgumentError,
181+
);
182+
});
169183
}
170184

185+
Future<dynamic> validOnBackgroundMessage(Map<String, dynamic> message) async {}
186+
171187
class MockMethodChannel extends Mock implements MethodChannel {}

0 commit comments

Comments
 (0)