我假设我遵循了所有步骤来处理来自flutter中firebase的后台通知。我已经创建了一个顶级函数,我希望每当收到通知时都会触发它。然而,该函数从未被触发。
以下是顶层后台处理程序函数,它存在于我的主页小部件中,但不在类中:
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}');
}
这是我的主页init state,它调用一个函数来初始化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((中调用FirebaseMessaging.onBackgroundMessage,而不是在initState((中
try:在主函数之后
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
...
}
并移除WidgetsFlutterBinding.ensureInitialized((在firebaseMessagingBackgroundHandler中
onMessage
处理前台通知
onMessageOpenedApp
处理后台通知
如果您希望在应用程序处于后台时发出通知,请使用onMessageOpenedApp
添加FirebaseMessaging
的另一个功能块,而不是使用onMessage
来处理后台通知,如下所示:
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(){}
中调用。