diff --git a/packages/firebase_messaging/CHANGELOG.md b/packages/firebase_messaging/CHANGELOG.md index 92ce3f94741a..e142274bcfd1 100644 --- a/packages/firebase_messaging/CHANGELOG.md +++ b/packages/firebase_messaging/CHANGELOG.md @@ -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. @@ -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) self; } diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 4be723a0d77e..ba9d66d2783f 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -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 `/android/app/src/main/java//`. ``` package io.flutter.plugins.firebasemessagingexample; @@ -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 `/android/app/src/main/`. ``` ``` -1. Define a top level Dart method to handle background messages +1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages ``` Future myBackgroundMessageHandler(Map message) { if (message.containsKey('data')) { diff --git a/packages/firebase_messaging/lib/firebase_messaging.dart b/packages/firebase_messaging/lib/firebase_messaging.dart index de6c85e8e6df..d01441ef32ae 100644 --- a/packages/firebase_messaging/lib/firebase_messaging.dart +++ b/packages/firebase_messaging/lib/firebase_messaging.dart @@ -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( 'FcmDartService#start', { diff --git a/packages/firebase_messaging/pubspec.yaml b/packages/firebase_messaging/pubspec.yaml index feb8d4e29efc..5b931d982519 100644 --- a/packages/firebase_messaging/pubspec.yaml +++ b/packages/firebase_messaging/pubspec.yaml @@ -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 homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging -version: 6.0.0 +version: 6.0.1 flutter: plugin: diff --git a/packages/firebase_messaging/test/firebase_messaging_test.dart b/packages/firebase_messaging/test/firebase_messaging_test.dart index c0a47ba57366..c8d844b84518 100644 --- a/packages/firebase_messaging/test/firebase_messaging_test.dart +++ b/packages/firebase_messaging/test/firebase_messaging_test.dart @@ -97,13 +97,18 @@ void main() { final Completer onLaunch = Completer(); final Completer onResume = Completer(); - 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; @@ -166,6 +171,17 @@ void main() { verify(mockChannel.invokeMethod('setAutoInitEnabled', false)); }); + + test('configure bad onBackgroundMessage', () { + expect( + () => firebaseMessaging.configure( + onBackgroundMessage: (dynamic message) => Future.value(), + ), + throwsArgumentError, + ); + }); } +Future validOnBackgroundMessage(Map message) async {} + class MockMethodChannel extends Mock implements MethodChannel {}