@@ -54,7 +54,82 @@ Note: When you are debugging on Android, use a device or AVD with Google Play se
5454 <category android : name =" android.intent.category.DEFAULT" />
5555 </intent-filter >
5656 ```
57-
57+ #### Optionally handle background messages
58+
59+ > Background message handling is intended to be performed quickly. Do not perform
60+ long running tasks as they may not be allowed to finish by the Android system.
61+ See [ Background Execution Limits] ( https://developer.android.com/about/versions/oreo/background )
62+ for more.
63+
64+ By default background messaging is not enabled. To handle messages in the background:
65+
66+ 1 . Add an Application.java class to your app
67+
68+ ```
69+ package io.flutter.plugins.firebasemessagingexample;
70+
71+ import io.flutter.app.FlutterApplication;
72+ import io.flutter.plugin.common.PluginRegistry;
73+ import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
74+ import io.flutter.plugins.GeneratedPluginRegistrant;
75+ import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
76+
77+ public class Application extends FlutterApplication implements PluginRegistrantCallback {
78+ @Override
79+ public void onCreate() {
80+ super.onCreate();
81+ FlutterFirebaseMessagingService.setPluginRegistrant(this);
82+ }
83+
84+ @Override
85+ public void registerWith(PluginRegistry registry) {
86+ GeneratedPluginRegistrant.registerWith(registry);
87+ }
88+ }
89+ ```
90+ 1. Set name property of application in `AndroidManifest.xml`
91+ ```
92+ <application android:name=".Application" ...>
93+ ```
94+ 1. Define a top level Dart method to handle background messages
95+ ```
96+ Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
97+ if (message.containsKey('data')) {
98+ // Handle data message
99+ final dynamic data = message['data'];
100+ }
101+
102+ if (message.containsKey('notification')) {
103+ // Handle notification message
104+ final dynamic notification = message['notification'];
105+ }
106+
107+ // Or do other work.
108+ }
109+ ```
110+ Note: the protocol of `data` and `notification` are in line with the
111+ fields defined by a [RemoteMessage](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage).
112+ 1. Set `onBackgroundMessage` handler when calling `configure`
113+ ```
114+ _firebaseMessaging.configure(
115+ onMessage: (Map<String, dynamic> message) async {
116+ print("onMessage: $message");
117+ _showItemDialog(message);
118+ },
119+ onBackgroundMessage: myBackgroundMessageHandler,
120+ onLaunch: (Map<String, dynamic> message) async {
121+ print("onLaunch: $message");
122+ _navigateToItemDetail(message);
123+ },
124+ onResume: (Map<String, dynamic> message) async {
125+ print("onResume: $message");
126+ _navigateToItemDetail(message);
127+ },
128+ );
129+ ```
130+ Note: `configure` should be called early in the lifecycle of your application
131+ so that it can be ready to receive messages as early as possible. See the
132+ example app for a demonstration.
58133
59134### iOS Integration
60135
0 commit comments