@@ -41,9 +41,15 @@ public class FirebaseMessagingPlugin extends BroadcastReceiver
4141 public static void registerWith (Registrar registrar ) {
4242 final MethodChannel channel =
4343 new MethodChannel (registrar .messenger (), "plugins.flutter.io/firebase_messaging" );
44+ final MethodChannel backgroundCallbackChannel =
45+ new MethodChannel (
46+ registrar .messenger (), "plugins.flutter.io/firebase_messaging_background" );
4447 final FirebaseMessagingPlugin plugin = new FirebaseMessagingPlugin (registrar , channel );
4548 registrar .addNewIntentListener (plugin );
4649 channel .setMethodCallHandler (plugin );
50+ backgroundCallbackChannel .setMethodCallHandler (plugin );
51+
52+ FlutterFirebaseMessagingService .setBackgroundChannel (backgroundCallbackChannel );
4753 }
4854
4955 private FirebaseMessagingPlugin (Registrar registrar , MethodChannel channel ) {
@@ -99,7 +105,40 @@ private Map<String, Object> parseRemoteMessage(RemoteMessage message) {
99105
100106 @ Override
101107 public void onMethodCall (final MethodCall call , final Result result ) {
102- if ("configure" .equals (call .method )) {
108+ /* Even when the app is not active the `FirebaseMessagingService` extended by
109+ * `FlutterFirebaseMessagingService` allows incoming FCM messages to be handled.
110+ *
111+ * `FcmDartService#start` and `FcmDartService#initialized` are the two methods used
112+ * to optionally setup handling messages received while the app is not active.
113+ *
114+ * `FcmDartService#start` sets up the plumbing that allows messages received while
115+ * the app is not active to be handled by a background isolate.
116+ *
117+ * `FcmDartService#initialized` is called by the Dart side when the plumbing for
118+ * background message handling is complete.
119+ */
120+ if ("FcmDartService#start" .equals (call .method )) {
121+ long setupCallbackHandle = 0 ;
122+ long backgroundMessageHandle = 0 ;
123+ try {
124+ Map <String , Long > callbacks = ((Map <String , Long >) call .arguments );
125+ setupCallbackHandle = callbacks .get ("setupHandle" );
126+ backgroundMessageHandle = callbacks .get ("backgroundHandle" );
127+ } catch (Exception e ) {
128+ Log .e (TAG , "There was an exception when getting callback handle from Dart side" );
129+ e .printStackTrace ();
130+ }
131+ FlutterFirebaseMessagingService .setBackgroundSetupHandle (
132+ this .registrar .context (), setupCallbackHandle );
133+ FlutterFirebaseMessagingService .startBackgroundIsolate (
134+ this .registrar .context (), setupCallbackHandle );
135+ FlutterFirebaseMessagingService .setBackgroundMessageHandle (
136+ this .registrar .context (), backgroundMessageHandle );
137+ result .success (true );
138+ } else if ("FcmDartService#initialized" .equals (call .method )) {
139+ FlutterFirebaseMessagingService .onInitialized ();
140+ result .success (true );
141+ } else if ("configure" .equals (call .method )) {
103142 FirebaseInstanceId .getInstance ()
104143 .getInstanceId ()
105144 .addOnCompleteListener (
0 commit comments