我假设我遵循了在 flutter 中处理来自 firebase 的后台通知的所有步骤。我创建了一个 top-level 函数,希望在收到通知时触发该函数。但是,该函数永远不会被触发。
这是我的主页小部件中存在但在类之外的顶级后台处理程序函数:
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// ignore: avoid_print
print('A background message just showed up : ${message.messageId}');
// update SQLite
var result = await PageService.instance
.add(PageService.instance.convertToPage(message.data));
print('added to db: ${result}');
}
这是我的主页初始化状态,它调用函数来初始化 Firebase 消息:
@override
void initState() {
super.initState();
_initializeFirebaseMessaging();
}
然后这里也是在主页类中定义的 _initializeFirebaseMessaging 函数:
void _initializeFirebaseMessaging() {
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('new notification arrived');
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
// update SQLite
var result = await PageService.instance
.add(PageService.instance.convertToPage(message.data));
print('added to db: ${result}');
if (notification != null && android != null) {
// show notification
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
color: Colors.blue,
playSound: true,
icon: '@mipmap/ic_launcher',
),
));
}
});
}
onmessage.listen 工作正常,因为我在应用程序中收到通知并处理它们,但后台处理程序根本没有触发。
我将不胜感激任何帮助!
您必须在 main() 中而不是 initState() 中调用 FirebaseMessaging.onBackgroundMessage
onMessage
处理前台通知
onMessageOpenedApp
处理后台通知
如果您想在应用程序处于
background时收到通知,请使用
onMessage
添加另一个 FirebaseMessaging
功能块,而不是使用 onMessageOpenedApp
来处理后台通知,如下所示:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message){...})
onMessage
仅当应用程序位于前台时才有效。
如果您希望在应用程序处于“终止”状态时收到通知,您可以使用:
FirebaseMessaging.instance.getInitialMessage().then((message){...})
将您的
firebaseMessagingBackgroundHandler
更新为:
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
// ignore: avoid_print
print('A background message just showed up : ${message.messageId}');
// update SQLite
var result = await PageService.instance
.add(PageService.instance.convertToPage(message.data));
print('added to db: ${result}');
}
最后,main()
应该看起来像这样:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Add Firebase Initialization:
await Firebase.initializeApp();
// This will initialize a new Firebase instance.
// Background message handler
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
runApp(const App());
}
您应该从main()
中调用
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
,而不是从主页 initState(){}
。Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
...
}
并删除firebaseMessagingBackgroundHandler
中的 WidgetsFlutterBinding.ensureInitialized();