我在模拟器上构建应用程序时收到此错误日志。
即使应用程序定期启动,这个致命错误也会在 main.dart 执行时出现:
E/FlutterFcmService( 6350): Fatal: failed to find callback
问题是没有执行通知,因为当我启动应用程序而不推送任何通知时,这个错误就出现了。 任何输入? 我使用的:
firebase_messaging: ^7.0.3
flutter_local_notifications: ^4.0.1
这是处理通知的代码,但错误发生在它执行之前:
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
registerNotification();
configLocalNotification();
}
void registerNotification() {
firebaseMessaging.requestNotificationPermissions();
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
print('onMessage: $message');
Platform.isAndroid
? showNotification(message['notification'])
: showNotification(message['aps']['alert']);
return ;
}, onResume: (Map<String, dynamic> message) {
print('onResume: $message');
return Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationsScreen()));
}, onLaunch: (Map<String, dynamic> message) {
print('onLaunch: $message');
return;
},
onBackgroundMessage: myBackgroundMessageHandler
);
firebaseMessaging.getToken().then((token) {
print('token: $token');
FirebaseFirestore.instance
.collection('Consultant')
.doc(firebaseUser.uid)
.update({'deviceToken': token});
}).catchError((err) {
//Fluttertoast.showToast(msg: err.message.toString());
});
}
Future selectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => NotificationsScreen(payload: payload,)),
);
}
void showNotification(message) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
Platform.isAndroid
? 'it.wytex.vibeland_pro_app'
: 'it.wytex.vibeland_pro_app',
'Vibeland Pro',
'Vibeland Pro',
playSound: true,
enableVibration: true,
importance: Importance.max,
priority: Priority.high,
icon: '@drawable/ic_stat_vibeland_pro'
);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
print(message);
print(message['body'].toString());
print(json.encode(message));
// await flutterLocalNotificationsPlugin.show(2, message['title'].toString(),
// message['body'].toString(), platformChannelSpecifics,
// payload: json.encode(message));
await flutterLocalNotificationsPlugin.show(
3, '? Hai ricevuto un messaggio ? ', 'Controlla subito le Tue notifiche ??', platformChannelSpecifics,
payload: '@drawable/ic_stat_vibeland_pro',
);
}
void configLocalNotification() {
var initializationSettingsAndroid =
new AndroidInitializationSettings('@drawable/ic_stat_vibeland_pro');
var initializationSettingsIOS = new IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
var initializationSettings = new InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
和后台消息在类的外部处理:
Future<void> myBackgroundMessageHandler(message) async {
print("Handling a background message: ${message}");
}
这在添加到 Firebase 的函数下方
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const fcm = admin.messaging();
exports.sendNotification = functions.firestore
.document("Notifications/{id}")
.onCreate((snapshot) => {
const name = snapshot.get("name");
const subject = snapshot.get("subject");
const token = snapshot.get("token");
const payload = {
notification: {
title: "" + name,
body: "" + subject,
sound: "default",
icon: "@drawable/ic_stat_vibeland_pro",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
};
return fcm.sendToDevice(token, payload);
});
不是我得到的错误是在我们调用这些函数之前...