我的firebase_messaging 9.1.0在应用程序处于前台、后台和关闭时接收并显示我的Android和iOS应用程序通知。然而,在iOS上,后台处理程序future似乎没有执行。我正在运行firebase_messaging_example应用程序,它在后台运行时不会打印"处理后台消息${message.messageId}"。
后台获取和远程通知在xcode中进行检查。
我试过:
发送通知时可单击和不可单击操作:";FLUTER_ NOTIFICATION_;
有无
<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());
}
请告诉我之后会发生什么,但这很可能会解决你的问题。