【问题标题】:Firebase Cloud Messaging doesn't work only when releasedFirebase 云消息传递仅在发布时才起作用
【发布时间】:2023-03-10 00:59:01
【问题描述】:

我已经在我的 Flutter 应用中实现了 Firebase Cloud Messaging,并且在大多数情况下它似乎都可以工作。我可以在以下情况下在我的应用中接收通知:

  • 使用flutter run
  • 使用flutter run --release
  • 使用 Xcode 的产品 > 运行

全部针对我的与计算机相连的 iPhone。我正在使用风味(dev、qa、prod),它适用于所有风味。

当我为 Firebase App Distribution 或 Testflight 打包它时,它不起作用。专注于应用分发更容易,所以我将介绍我在那里所做的事情。

在设置消息传递时,我在 XCode 中启用了推送通知(我最初遇到了点击开发 APNs 的问题,但我更改了配置以使用生产服务)。这是我的授权文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
    <key>com.apple.developer.applesignin</key>
    <array>
        <string>Default</string>
    </array>
    <key>com.apple.developer.associated-domains</key>
    <array>
        <string>applinks:N/A</string>
    </array>
</dict>
</plist>

Info.plist 文件中也启用了背景模式:

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

我根据需要在 Apple Developer Portal 中创建了一个密钥:

我已根据需要将其上传到 Firebase:

在部署到 App Distribution 时,我运行以下命令:

flutter build ios --flavor <env>
cd ios
xcodebuild -workspace Runner.xcworkspace -scheme <env> archive -archivePath Runner.xcarchive
xcodebuild -exportArchive -archivePath Runner.xcarchive -exportOptionsPlist exportOptions.plist -exportPath ../build/ios -allowProvisioningUpdates

如上所述,我的风格之一是哪里,而我的 exportOptions.plist 是

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>compileBitcode</key>
    <false/>
    <key>method</key>
    <string>ad-hoc</string>
</dict>
</plist>

我很乐意提供更多线索、配置和解释,但现在,我不知道缺少什么以及可能导致这种行为。所以问题是,为什么通过存档部署推送通知不起作用?

【问题讨论】:

  • 我应该注意到,是的,我使用的是 firebase_messaging 7.0.3。我知道有一个相当大的大修的开发版本,但由于我要去生产,好吧,昨天,我不能将非生产版本投入生产。

标签: ios flutter firebase-cloud-messaging


【解决方案1】:

TLDR:我的 AppDelegate.swift 中缺少 application.registerForRemoteNotifications()

看起来我的 AppDelegate.swift 会有助于发布:

@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 flavorChannel = FlutterMethodChannel(name: "flavor", binaryMessenger: controller.binaryMessenger)
    flavorChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
        result(Bundle.main.infoDictionary?["Flavor"])
    })
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我们已根据 firebase_messaging 项目 (https://pub.dev/packages/firebase_messaging) 中的 README 实施了这些指示。这需要添加if #available 行。是的,这只是为了进行方法调配,而我们没有(即我们的 Info.plist 文件中没有FirebaseAppDelegateProxyEnabled),但我偶然发现了Flutter FCM on iOS 14(他们正在实施调配),并注意到他们的文件中多了一行:application.registerForRemoteNotifications()。一旦我添加了那一行(在返回之前的那一行),推送通知就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多