Я использую https://pub.dev/packages/firebase_messaging v6.0.16 и flutter v1.17.5
[√] 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)
И я пытаюсь вызвать плагин внутри myBackgroundMessageHandler в строке, где написано AppAvailability.launchApp('com.companyname.appname');
class CloudMessagingService extends NotificationService {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
void configure() {
if (Platform.isIOS) _firebaseMessaging.requestNotificationPermissions(IosNotificationSettings());
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
if (isDataNotification(message)) return;
print('onMessage');
},
onLaunch: (Map<String, dynamic> message) async {
if (isDataNotification(message)) return;
print('onLaunch');
},
onResume: (Map<String, dynamic> message) async {
if (isDataNotification(message)) return;
print('onResume');
},
onBackgroundMessage: myBackgroundMessageHandler,
);
}
static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> 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;
}
Но я получаю следующую ошибку:
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): <asynchronous suspension>
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.<anonymous closure>
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.<anonymous closure>
package:flutter/…/services/platform_channel.dart:377
E/flutter (28000): #7 _DefaultBinaryMessenger.handlePlatformMessage
package:flutter/…/services/binding.dart:199
E/flutter (28000): #8 _invoke3.<anonymous closure> (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)
Плагины работают без проблем в области приложения. Это происходит с каждым плагином, который я пробовал!
Итак, я думаю, проблема в том, что изолятор, который создает FCM, не обернут механизмом флаттера, как я понял из этого поста.
https://github.com/flutter/flutter/issues/13937#issuecomment -656239596
, но вам нужно создать собственный класс приложения, который подключается к инициализации фона FlutterNativeView плагина.
Я сделал следуйте инструкциям и измените мой Application.kt на следующие
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("io.flutter.plugins.firebasemessaging")) {
FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
if (!registry!!.hasPlugin("in.jvapps.system_alert_window")) {
SystemAlertWindowPlugin.registerWith(registry!!.registrarFor("in.jvapps.system_alert_window"));
}
}
fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "groupChannel";
val descriptionText = "This is the group channel";
val importance = NotificationManager.IMPORTANCE_HIGH;
val mChannel = NotificationChannel("59054", name, importance);
mChannel.description = descriptionText;
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager;
notificationManager.createNotificationChannel(mChannel);
}
}
}
и мой MainActivity.kt
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);
}
}
И мой AndroidManifest. xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.companyname.appname">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE " />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 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. -->
<application
android:name=".Application"
android:label="App Name"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
</application>
</manifest>
Кажется, что уведомления отображаются правильно как уведомления, и я могу вызвать свой BackgroundMessageHandler и выполнить код, просто я не могу ничего сделать с каким-либо плагином в myBackgroundMessageHandler. Правильно ли я реализовал это?
Он всегда будет выдавать ошибку в этой строке AppAvailability.launchApp('com.companyname.appname');
в myBackgroundMessageHandler
, потому что канал метода не может быть вызван изолятором, если плагин не зарегистрирован.
Я также проверил этот пост на github
Аналогичная проблема
Но мой Application.kt настроен так
Я попытался очистить флаттер - > удаление приложения -> установка приложения. И проблема остается. Как они описывают здесь
MissingPluginException (Реализация для показа метода на канале dexterous.com/flutter/local_notifications не найдена)
EDIT: Когда я используйте плагин DeviceApp, происходит то же самое. Я заменил
AppAvailability.launchApp('com.companyname.appname');
на
DeviceApps.openApp('com.companyname.appname');
Итак, я добавил следующие 2 строки в метод registerWith Application.kt, и это, кажется, вернуло исключение отсутствующего плагина
if (!registry!!.hasPlugin("fr.g123k.deviceapps")) {
DeviceAppsPlugin.registerWith(registry!!.registrarFor("fr.g123k.deviceapps"));
}
Но ошибка меняется на
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): <asynchronous suspension>
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.<anonymous closure>
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.<anonymous closure>
package:flutter/…/services/platform_channel.dart:377
E/flutter (25583): #8 _DefaultBinaryMessenger.handlePlatformMessage
package:flutter/…/services/binding.dart:199
E/flutter (25583): #9 _invoke3.<anonymous closure> (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)
Итак, теперь он больше не говорит, что плагин отсутствует, а говорит: PlatformException(error, Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()
Изменить 2:
Я только что заметил, что последняя ошибка https://github.com/g123k/flutter_plugin_device_apps/issues/31 может быть связана с самим плагином. Посмотрим, работают ли сейчас разные плагины
Надеюсь, я предоставил достаточно информации, пожалуйста, дайте мне знать, нужна ли дополнительная информация или если вопрос нужно изменить. Я могу ответить в течение пары минут.