【问题标题】:firebase-messaging - Fatal: failed to find callbackfirebase-messaging - 致命:找不到回调
【发布时间】:2020-01-29 02:17:31
【问题描述】:

我正在尝试使用 FCM 消息传递并不断收到此错误。

E/FlutterFcmService( 3684): Fatal: failed to find callback

下面是我用来设置的代码。

  static Future<void> messagePiper(
      Map<String, dynamic> message,
      FilteredMap<String, ChatMessage> globalChatEntryMap,
      FilteredMap<String, ChatMessage> gameChatEntryMap,
      Subject<List<ChatMessage>> globalChatSubject,
      Subject<List<ChatMessage>> gameChatSubject,
      Map<String, Player> _playerMap) async {
    final Map<String, dynamic> data = message['data'];
    if (data.containsKey('name')) {
      final msg = ChatMessage.fromMap(data);
      globalChatEntryMap.putIfAbsent(msg.id, () => msg);
      globalChatSubject.add(globalChatEntryMap.values.toList());
    } else {
      final msg = GameChatMessage.fromMap(data);

      final chat = ChatMessage.fromGlobalChatMessage(
        msg,
        _playerMap[msg.pId].name,
        _playerMap[msg.pId].imageUrl,
      );

      print('chat: $chat');
      gameChatEntryMap.putIfAbsent(msg.id, () => chat);
      print('_gameChatEntryMap : $gameChatEntryMap');
      gameChatSubject.add(gameChatEntryMap.values.toList());
    }

    return Future<void>.value();
  }

是传入FirebaseMessaging配置的回调。

  final FirebaseMessaging _fm = FirebaseMessaging();

  @override
  void initState() {

   _fm.configure(
        onMessage: (Map<String, dynamic> message) async {
          print('onMessagee : $message');
          return Utils.messagePiper(
              message,
              _globalChatEntryMap,
              _gameChatEntryMap,
              _globalChatSubject,
              _gameChatSubject,
              _playerMap);
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('onLaunch : $message');
          return Utils.messagePiper(
              message,
              _globalChatEntryMap,
              _gameChatEntryMap,
              _globalChatSubject,
              _gameChatSubject,
              _playerMap);
          ;
        },
        onResume: (Map<String, dynamic> message) async {
          print('onResume : $message');
          return Utils.messagePiper(
              message,
              _globalChatEntryMap,
              _gameChatEntryMap,
              _globalChatSubject,
              _gameChatSubject,
              _playerMap);
          ;
        },
        onBackgroundMessage: null);
....

Java 配置文件

package io.flutter.plugins;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

依赖版本

  random_string: 0.0.2
  firebase_auth: ^0.14.0+5
  firebase_database: ^3.0.7
  provider: 3.0.0
  rxdart: ^0.22.2
  collection: ^1.14.11
  audioplayers: ^0.13.2
  firebase_admob: ^0.5.5
  connectivity: ^0.4.4
  firebase_messaging: ^5.1.6 # tried with several different versions

我尝试了几个firebase_messaging 版本,但找不到修复方法。

感谢任何帮助解决此问题。

【问题讨论】:

  • 你怎么有自定义的Application类却没有onBackgroundMessage回调?如果要使用后台消息传递,则只需要自定义 java 文件。
  • Java 文件? MainActivity.kt 导入了吗? ``` import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService ``` 并把它? ```flutterEngine.plugins.add(FlutterFirebaseMessagingService()) ``` 在configureFlutterEngine中?

标签: firebase flutter firebase-cloud-messaging


【解决方案1】:

此错误消息来自startBackgroundIsolate,用于允许处理后台消息。 如果您不想处理后台消息,则可以放心地忽略此错误消息。否则,您需要设置回调来处理后台消息,如here 所述

如果点击通知时你的回调没有被执行,那是因为你没有将消息的click_action属性设置为FLUTTER_NOTIFICATION_CLICK

【讨论】:

  • 我在通知中发送click_action :FLUTTER_NOTIFICATION_CLICK 在 onMessage 回调中仍然出现错误:```致命:找不到回调```
  • Me too Android 只是找不到回调
  • @shinriyo 如所说,如果您不打算处理后台消息,则应忽略此“找不到回调”错误消息。如果你想摆脱这个错误消息,那么你需要设置一个回调来处理后台消息
【解决方案2】:

您是否使用网络发送 FCM,而不是 FCM 控制台? 确保发布请求在您的后端是正确的。我正在使用 Laravel

$response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization'=> 'key='. $token,
        ])->post($url, [
            'notification' => [
                'body' => $request->summary,
                'title' => $request->title,
                'image' => request()->getHttpHost().$path,
                
            ],
            'priority'=> 'high',
            'data' => [
                'click_action'=> 'FLUTTER_NOTIFICATION_CLICK',
                
                'status'=> 'done',
                
            ],
            'to' => '/topics/all'
        ]);

【讨论】:

  • 因此只需将“click_action”参数添加到数据中即可。
【解决方案3】:

你应该声明一个类外的 backgroundMessageHandler 函数或静态函数,以便从外部访问,然后将此函数传递给 fbm.configure:

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  print('on background $message');
}

fbm.configure(
      onMessage: (msg) {
        print(msg);
        return;
      },
      onLaunch: (msg) {
        print(msg);
        return;
      },
      onResume: (msg) {
        print(msg);
        return;
      },
      onBackgroundMessage: myBackgroundMessageHandler
);

同时打开 your_project_folder/android/app/source/AndroidManifest.xml 并将此 XML 代码粘贴到主 Activity 的现有意图过滤器代码之后:

<intent-filter>
    <action android:name="FLUTTER_NOTIFICATION_CLICK" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

结果将类似于以下代码:

<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>

【讨论】:

    【解决方案4】:

    Application 类中使用下面的代码

    以前是GeneratedPluginRegistrant.registerWith(registry);

    替换为 FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));

    import io.flutter.app.FlutterApplication;
    import io.flutter.plugin.common.PluginRegistry;
    import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
    import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
    
    public class Application extends FlutterApplication implements PluginRegistrantCallback {
    
        @Override
        public void onCreate(){
            super.onCreate();
            FlutterFirebaseMessagingService.setPluginRegistrant(this);
        }
    
        @Override
        public void registerWith(PluginRegistry registry){
            FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    
    
    }
    

    【讨论】:

      【解决方案5】:

      当我尝试从 Firebase 控制台发送通知以测试我的应用接收通知时,我也遇到了同样的错误。确保你在你的模拟器上杀死了这个应用程序,这样它就不会在后台运行。现在尝试从 firebase 控制台发送通知,等待几秒钟,您应该会看到它。这对我有用。

      【讨论】:

        猜你喜欢
        • 2020-12-08
        • 2021-08-04
        • 2021-07-23
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        相关资源
        最近更新 更多