我设置了 Firebase 通知,它们有开放式和封闭式两种。 这是代码示例
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('ok');
}
Future<void> main() async {
AppMetrica.runZoneGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
await AppMetrica.activate(config.appMetricaConfig);
HttpOverrides.global = MyHttpOverrides();
Workmanager().initialize(callbackDispatcher);
...
...
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
runApp(AppContainer());
});
}
但是firebaseMessagingBackgroundHandler里面的代码不想被执行。 我正在 iOS 上测试此问题,但问题在 Android 上重复出现。 已授予所有通知权限。
如果通知出现在最小化或关闭的应用程序中,则必须执行特定的代码。例如,发送 post 请求。
Future<void> backgroundNotification() async {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (kDebugMode) {
print("message ===> $message");
print("message ===> ${message.sentTime}");
print("message ===> ${message.notification?.title}");
print("message ===> ${message.notification?.body}");
}
if (message.notification!.title!.isNotEmpty &&
message.notification!.body!.isNotEmpty) {
// Can use package:flutter_local_notifications for show notifications in app.
}
});