Flutter后台推送通知不适用于iOS



我看了所有其他的问题和答案,但我无法解决我的问题,即使我尝试了所有的方法。

当应用程序在IOS和Android的前台时,我可以收到通知。但如果我在IOS的后台,我需要进入应用程序才能看到通知。

main.dart initState 代码

@override
void initState() {
super.initState();
_firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.requestNotificationPermissions(
IosNotificationSettings(sound: true, badge: true, alert: true));
print("requested");
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
print("token");
print(token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
//showNotification(message);
},
onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
// this will never called, but it's ok
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
// this will never called, but it's ok
print("onResume: $message");
},
);
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
var androidInitialize = AndroidInitializationSettings('app_icon');
var iosInitialize = IOSInitializationSettings();
var initializationSettings =
InitializationSettings(androidInitialize, iosInitialize);
//flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: notificationSelected);
}

我在我的info.plist.中有这些与通知相关的密钥

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
<string>remote-notification</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

我的AppDelagete.swift文件

import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if FirebaseApp.app() == nil { FirebaseApp.configure()}
// if #available(iOS 10.0, *) {
//   UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
// }
// Messaging.messaging().apnsToken = deviceToken
// super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)

if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

我已经尝试用这个代码发送通知

DATA='{"notification": {"body": "Body","title": "Title Title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done", "priority":"high"}, "to": "<TOKEN>"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=<SERVERKEY>"

非常感谢。。。

您需要启用"背景模式";Flutter应用程序在iOS上后台接收消息的设置功能。有关更多详细信息,请参阅本指南。

相关内容

  • 没有找到相关文章

最新更新