title: How do I correctly register a plugin with the FCM plugin
body: <p>I am using <a href="https://pub.dev/packages/firebase_messaging" rel="nofollow noreferrer">https://pub.dev/packages/firebase_messaging</a> v6.0.16 and flutter v1.17.5</p>
  <pre><code>[√] Flutter (Channel stable, v1.17.5, on Microsoft Windows [Version 10.0.18362.900], locale en-US)
   
  [√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
  [√] Android Studio (version 4.0)
  [√] VS Code (version 1.47.0)
  </code></pre>
  <p>And I am trying to call a plugin inside my myBackgroundMessageHandler on the line where it says <code>AppAvailability.launchApp('com.companyname.appname');</code></p>
  <pre><code>class CloudMessagingService extends NotificationService {
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  
    void configure() {
      if (Platform.isIOS) _firebaseMessaging.requestNotificationPermissions(IosNotificationSettings());
  
      _firebaseMessaging.configure(
        onMessage: (Map&lt;String, dynamic&gt; message) async {
          if (isDataNotification(message)) return;
          print('onMessage');
        },
        onLaunch: (Map&lt;String, dynamic&gt; message) async {
          if (isDataNotification(message)) return;
          print('onLaunch');
        },
        onResume: (Map&lt;String, dynamic&gt; message) async {
          if (isDataNotification(message)) return;
          print('onResume');
        },
        onBackgroundMessage: myBackgroundMessageHandler,
      );
    }
  
    static Future&lt;dynamic&gt; myBackgroundMessageHandler(Map&lt;String, dynamic&gt; message) async {
      print('on background message');
      AppAvailability.launchApp('com.companyname.appname'); // it will throw an error when it tries to call the method channel in the app availability plugin
      return message;
    }
  </code></pre>
  <p>But I get the following error</p>
  <pre><code>E/flutter (28000): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method launchApp on channel com.pichillilorenzo/flutter_appavailability)
  E/flutter (28000): #0      MethodChannel._invokeMethod 
  package:flutter/…/services/platform_channel.dart:154
  E/flutter (28000): &lt;asynchronous suspension&gt;
  E/flutter (28000): #1      MethodChannel.invokeMethod 
  package:flutter/…/services/platform_channel.dart:329
  E/flutter (28000): #2      AppAvailability.launchApp 
  package:flutter_appavailability/flutter_appavailability.dart:95
  E/flutter (28000): #3      CloudMessagingService.myBackgroundMessageHandler 
  package:moto_picto/…/firebase/cloud_messaging_service.dart:43
  E/flutter (28000): #4      _fcmSetupBackgroundChannel.&lt;anonymous closure&gt; 
  package:firebase_messaging/firebase_messaging.dart:38
  E/flutter (28000): #5      MethodChannel._handleAsMethodCall 
  package:flutter/…/services/platform_channel.dart:409
  E/flutter (28000): #6      MethodChannel.setMethodCallHandler.&lt;anonymous closure&gt; 
  package:flutter/…/services/platform_channel.dart:377
  E/flutter (28000): #7      _DefaultBinaryMessenger.handlePlatformMessage 
  package:flutter/…/services/binding.dart:199
  E/flutter (28000): #8      _invoke3.&lt;anonymous closure&gt;  (dart:ui/hooks.dart:290:15)
  E/flutter (28000): #9      _rootRun  (dart:async/zone.dart:1184:13)
  E/flutter (28000): #10     _CustomZone.run  (dart:async/zone.dart:1077:19)
  E/flutter (28000): #11     _CustomZone.runGuarded  (dart:async/zone.dart:979:7)
  E/flutter (28000): #12     _invoke3  (dart:ui/hooks.dart:289:10)
  E/flutter (28000): #13     _dispatchPlatformMessage  (dart:ui/hooks.dart:164:5)
  </code></pre>
  <p>The plugins work without any trouble in the app scope. This is happening for every plugin I tried!</p>
  <p>So I think the problem is that the isolate that FCM makes isn't wrapped with the flutter engine from what I understand from this post.</p>
  <p><a href="https://github.com/flutter/flutter/issues/13937#issuecomment-656239596" rel="nofollow noreferrer">https://github.com/flutter/flutter/issues/13937#issuecomment-656239596</a></p>
  <blockquote>
  <p>but you need to create a custom Application class that hooks into the
  initialisation of the plugin's background FlutterNativeView.</p>
  </blockquote>
  <p>I did follow the instructions and changed my Application.kt to the following</p>
  <pre><code>package com.companyname.appname
      
  import `in`.jvapps.system_alert_window.SystemAlertWindowPlugin
  import io.flutter.app.FlutterApplication
  import io.flutter.plugin.common.PluginRegistry
  import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
  import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
  import io.flutter.view.FlutterMain
  import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
  
  import android.os.Build
  import android.app.NotificationManager
  import android.app.NotificationChannel
  
  public class Application: FlutterApplication(), PluginRegistrantCallback {
  
     override fun onCreate() {
       super.onCreate();
       FlutterFirebaseMessagingService.setPluginRegistrant(this);
       createNotificationChannels();
       SystemAlertWindowPlugin.setPluginRegistrant(this);
       FlutterMain.startInitialization(this);
     }
  
     override fun registerWith(registry: PluginRegistry) {
      if (!registry!!.hasPlugin(&quot;io.flutter.plugins.firebasemessaging&quot;)) {
        FirebaseMessagingPlugin.registerWith(registry!!.registrarFor(&quot;io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin&quot;));
      }
      if (!registry!!.hasPlugin(&quot;in.jvapps.system_alert_window&quot;)) {
        SystemAlertWindowPlugin.registerWith(registry!!.registrarFor(&quot;in.jvapps.system_alert_window&quot;));
      }
     }
  
     fun createNotificationChannels() {
       if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
          val name = &quot;groupChannel&quot;;
          val descriptionText = &quot;This is the group channel&quot;;
          val importance = NotificationManager.IMPORTANCE_HIGH;
          val mChannel = NotificationChannel(&quot;59054&quot;, name, importance);
          mChannel.description = descriptionText;
          val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager;
          notificationManager.createNotificationChannel(mChannel);
      }
    }
  }
  </code></pre>
  <p>and my MainActivity.kt</p>
  <pre><code>package com.companyname.appname
  
  import androidx.annotation.NonNull;
  import io.flutter.embedding.android.FlutterActivity
  import io.flutter.embedding.engine.FlutterEngine
  import io.flutter.plugins.GeneratedPluginRegistrant
  
  class MainActivity: FlutterActivity() {
      override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
          GeneratedPluginRegistrant.registerWith(flutterEngine);
      }
  }
  </code></pre>
  <p>And my AndroidManifest.xml</p>
  <pre><code>&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.companyname.appname&quot;&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_CONTACTS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.SYSTEM_ALERT_WINDOW&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.FOREGROUND_SERVICE &quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
      &lt;!-- io.flutter.app.FlutterApplication is an android.app.Application that
           calls FlutterMain.startInitialization(this); in its onCreate method.
           In most cases you can leave this as-is, but you if you want to provide
           additional functionality it is fine to subclass or reimplement
           FlutterApplication and put your custom class here. --&gt;
      &lt;application
          android:name=&quot;.Application&quot;
          android:label=&quot;App Name&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;&gt;
          &lt;activity
              android:name=&quot;.MainActivity&quot;
              android:launchMode=&quot;singleTop&quot;
              android:theme=&quot;@style/LaunchTheme&quot;
              android:configChanges=&quot;orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode&quot;
              android:hardwareAccelerated=&quot;true&quot;
              android:windowSoftInputMode=&quot;adjustResize&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;FLUTTER_NOTIFICATION_CLICK&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
          android:name=&quot;com.yalantis.ucrop.UCropActivity&quot;
          android:screenOrientation=&quot;portrait&quot;
          android:theme=&quot;@style/Theme.AppCompat.Light.NoActionBar&quot;/&gt;
          &lt;!-- Don't delete the meta-data below.
               This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --&gt;
          &lt;meta-data
              android:name=&quot;flutterEmbedding&quot;
              android:value=&quot;2&quot; /&gt;
          &lt;meta-data
              android:name=&quot;com.google.firebase.messaging.default_notification_channel_id&quot;
              android:value=&quot;@string/default_notification_channel_id&quot;/&gt;  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  <p>The notifications seem to be appearing correctly as heads up notifications, and I can call my BackgroundMessageHandler and execute code, it's just that I cannot do anything with any plugin in the myBackgroundMessageHandler. Did I implement it correctly?</p>
  <p>It will always throw the error on this line <code>AppAvailability.launchApp('com.companyname.appname');</code> in the <code>myBackgroundMessageHandler</code> because a method channel cannot be called by a isolate if the plugin is not registered.</p>
  <p>I also checked out this github post</p>
  <p><a href="https://github.com/MaikuB/flutter_local_notifications/issues/238#issuecomment-629749050" rel="nofollow noreferrer">Similar problem</a></p>
  <p>But my Application.kt is setup like that</p>
  <p>I tried doing flutter clean -&gt; uninstalling the app -&gt; installing the app as well. And the problem remains. Like they describe here</p>
  <p><a href="https://stackoverflow.com/questions/60471697/missingpluginexceptionno-implementation-found-for-method-show-on-channel-dexter/60576958#60576958">MissingPluginException(No implementation found for method show on channel dexterous.com/flutter/local_notifications)</a></p>
  <p><strong>EDIT:</strong> When I use the DeviceApp plugin the same thing happens. I replaced</p>
  <pre><code>AppAvailability.launchApp('com.companyname.appname');
  </code></pre>
  <p>with</p>
  <pre><code> DeviceApps.openApp('com.companyname.appname');
  </code></pre>
  <p>So I added the following 2 lines to the registerWith method of Application.kt, and it seems to revolve the missing plugin exception</p>
  <pre><code>if (!registry!!.hasPlugin(&quot;fr.g123k.deviceapps&quot;)) {
    DeviceAppsPlugin.registerWith(registry!!.registrarFor(&quot;fr.g123k.deviceapps&quot;));
  }
  </code></pre>
  <p>But the error changes to</p>
  <pre><code>E/flutter (25583): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()' on a null object reference, null)
  E/flutter (25583): #0      StandardMethodCodec.decodeEnvelope 
  package:flutter/…/services/message_codecs.dart:569
  E/flutter (25583): #1      MethodChannel._invokeMethod 
  package:flutter/…/services/platform_channel.dart:156
  E/flutter (25583): &lt;asynchronous suspension&gt;
  E/flutter (25583): #2      MethodChannel.invokeMethod 
  package:flutter/…/services/platform_channel.dart:329
  E/flutter (25583): #3      DeviceApps.openApp 
  package:device_apps/device_apps.dart:81
  E/flutter (25583): #4      CloudMessagingService.myBackgroundMessageHandler 
  package:moto_picto/…/firebase/cloud_messaging_service.dart:73
  E/flutter (25583): #5      _fcmSetupBackgroundChannel.&lt;anonymous closure&gt; 
  package:firebase_messaging/firebase_messaging.dart:38
  E/flutter (25583): #6      MethodChannel._handleAsMethodCall 
  package:flutter/…/services/platform_channel.dart:409
  E/flutter (25583): #7      MethodChannel.setMethodCallHandler.&lt;anonymous closure&gt; 
  package:flutter/…/services/platform_channel.dart:377
  E/flutter (25583): #8      _DefaultBinaryMessenger.handlePlatformMessage 
  package:flutter/…/services/binding.dart:199
  E/flutter (25583): #9      _invoke3.&lt;anonymous closure&gt;  (dart:ui/hooks.dart:290:15)
  E/flutter (25583): #10     _rootRun  (dart:async/zone.dart:1184:13)
  E/flutter (25583): #11     _CustomZone.run  (dart:async/zone.dart:1077:19)
  E/flutter (25583): #12     _CustomZone.runGuarded  (dart:async/zone.dart:979:7)
  E/flutter (25583): #13     _invoke3  (dart:ui/hooks.dart:289:10)
  E/flutter (25583): #14     _dispatchPlatformMessage  (dart:ui/hooks.dart:164:5)
  </code></pre>
  <p>So now it is no longer saying plugin missing but it is saying <code>PlatformException(error, Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()</code></p>
  <p><strong>Edit 2:</strong></p>
  <p>I just noticed that the latest error <a href="https://github.com/g123k/flutter_plugin_device_apps/issues/31" rel="nofollow noreferrer">https://github.com/g123k/flutter_plugin_device_apps/issues/31</a> might be one of the plugin itself. Going to see if different plugins work now</p>
  <p>I hope I provided enough info, please let me know if more info is needed or if the question needs to altered. I can respond within a couple off minutes.</p>