1

直到编写 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 和 swift 代码之间进行通信。

这是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。

我想要的是在数据消息到达时进行后台工作(保存在我的本地数据库中)。

4

0 回答 0