问题
I'm trying to use FCM messaging and keep getting this error.
E/FlutterFcmService( 3684): Fatal: failed to find callback
Below is the code I've used to setup.
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();
}
is the callback passed in to FirebaseMessaging configuration.
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 configuration file
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);
}
}
dependency versions
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
I tried with several firebase_messaging versions but couldn't find a fix.
Appreciate any help to solve this issue.
回答1:
This error message is coming from startBackgroundIsolate which is used for allowing handling background messages.
If you don't want to handle background messages then you can safely ignore this error message. Otherwise, you need to set up a callback for handling background messages as described here
If your callback is not executed when clicking on the notification then it's because you didn't set click_action property of the message to FLUTTER_NOTIFICATION_CLICK
回答2:
Are you sending FCM using web not FCM console? make sure the post is correct. I'm using 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'
]);
来源:https://stackoverflow.com/questions/58156832/firebase-messaging-fatal-failed-to-find-callback