firebase-messaging-致命:无法找到回调

时间:2019-09-29 15:51:29

标签: firebase flutter firebase-cloud-messaging

我正在尝试使用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 version

我尝试了多个firebase_messaging版本,但找不到修复程序。

感谢您提供帮助以解决此问题。

5 个答案:

答案 0 :(得分:4)

此错误消息来自startBackgroundIsolate,该错误消息用于允许处理后台消息。 如果您不想处理后台消息,则可以放心地忽略此错误消息。否则,您需要按照here

的说明设置一个用于处理后台消息的回调

如果在单击通知时未执行回调,那是因为您没有将消息的click_action属性设置为FLUTTER_NOTIFICATION_CLICK

答案 1 :(得分:3)

您是否使用Web发送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'
        ]);

答案 2 :(得分:0)

您应该声明一个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代码粘贴到主要活动的现有意图过滤器代码之后:

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

答案 3 :(得分:0)

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"));
    }


}

答案 4 :(得分:0)

当我试图从 firebase 控制台发送通知只是为了测试我的应用程序接收通知时,我也遇到了同样的错误。确保在模拟器上终止该应用程序,使其不在后台运行。现在尝试从 firebase 控制台发送通知,等待几秒钟,您应该会看到它。这对我有用。