直到编写FCM Flutter插件之时,iOS上的推送通知仍未实现后台处理。 我正在尝试使用本机代码(Swift)实现,但遇到了一些困难。
这是我的AppDelegate.swift:
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let _platformChannel = FlutterMethodChannel(name: "br.uff.uffmobileplus/uffmobile_channel",
binaryMessenger: controller as! FlutterBinaryMessenger)
_platformChannel.setMethodCallHandler({
//omitted code
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let notificationChannel = FlutterMethodChannel(name: "br.uff.uffmobileplus/notification_channel", binaryMessenger: controller as! FlutterBinaryMessenger)
notificationChannel.invokeMethod("saveToDataBase", arguments: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
}
(我省略了一些不相关的代码)
我看到了一些本地的iOS实现,他们做了一些类似的事情。我不是Swift程序员,所以根本不知道这是否正确。 发生了什么事
当远程数据或通知消息到达时,不会调用didReceiveRemoteNotification
。
我正在使用platform_channel在dart和快速代码之间进行通信。
这是json数据消息:
"\"data\": {"
"\"body\": \"$body\","
"\"title\": \"$title\","
"\"route\": \"$route\","
"\"sender\": \"$sender\","
"\"click_action\": \"FLUTTER_NOTIFICATION_CLICK\","
"\"mutable_content\": true,"
"\"content_available\": true"
"}, "
"\"priority\": \"high\","
"\"to\": "
"\"/topics/$group\""
是的,它写得很奇怪,但是可以用正确的信息触发onMessage,所以可以用。
我想要的是在数据消息到达时进行后台工作(保存在本地数据库中)。