我已经让 firebase_messaging 9.1.0 接收并显示我的 Android 和 iOS 应用程序在前台、后台和关闭时的通知。但是在 iOS 上,后台处理程序 future 似乎没有执行。我正在运行 firebase_messaging_example 应用程序,它在后台运行时不会打印“处理后台消息 ${message.messageId}”。
在 xcode 中检查后台获取和远程通知。
我试过:
使用和不使用 clickAction 发送通知:“FLUTER_NOTIFICATION_CLICK”
有无
<key>FirebaseAppDelegateProxyEnabled</key> <false/>
在 info.plist 中
在 xcode 中检查有和没有后台处理。
我希望后台处理程序执行一些在应用程序启动器图标上设置徽章的代码。有谁知道如何让处理程序与 iOS 一起工作,或者如何在应用程序关闭时在启动器图标上设置徽章?
最佳答案
根据您在评论区的回复,请尝试使用这个:
这是你的顶层函数,在 void main()
之上
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (Firebase.apps.isEmpty) await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
handleNotifications() async {
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(badge: true, alert: true, sound: true);//presentation options for Apple notifications when received in the foreground.
FirebaseMessaging.onMessage.listen((message) async {
print('Got a message whilst in the FOREGROUND!');
return;
}).onData((data) {
print('Got a DATA message whilst in the FOREGROUND!');
print('data from stream: ${data.data}');
});
FirebaseMessaging.onMessageOpenedApp.listen((message) async {
print('NOTIFICATION MESSAGE TAPPED');
return;
}).onData((data) {
print('NOTIFICATION MESSAGE TAPPED');
print('data from stream: ${data.data}');
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.instance
.getInitialMessage()
.then((value) => value != null ? _firebaseMessagingBackgroundHandler : false);
return;
}
然后, 在void main里面,这样写:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
handleNotifications();
runApp(MyAppState());
}
请告诉我之后会发生什么,但这很可能会解决您的问题。
关于Flutter firebase_messaging 后台处理程序不执行但消息到达 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66849209/