ios - iOS 无法通过 FCM 接收推送通知(可以通过 APNs)
问题描述
我正要在stackoverflow上问这个问题,但我只是想通了。为了后代,我还是发布了这个,因为这个错误花了我几天的时间,我在这个网站上找不到任何提及它的地方。
在制作了两种不同的方案并根据 AppDelegate 中的方案选择不同的 plist 后,我无法通过 FCM 发送推送通知。选择我的 plist 的代码如下所示:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
let defaults = UserDefaults.standard
// NOTE: DONT call registrationDataManager here or any other VC that uses Firebase before it's configured
let gcmMessageIDKey = "gcm.message_id"
let window: UIWindow? = nil
//XXX OVERRIDE INIT BREAKS SWIZZLING -- DO NOT USE
override init() {
super.init()
let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
var firebasePlistFileName = "GoogleService-Info"
if buildFor == "PROD" {
//firebasePlistFileName = "GoogleService-Info-Production"
firebasePlistFileName = "RELEASE-NEW-GoogleService-Info"
print("--- USING PRODUCTION / RELEASE PLIST")
}
else {
firebasePlistFileName = "DEBUG-NEW-GoogleService-Info"
print("--- USING DEBUG PLIST")
}
let filePath = Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist")
guard let fileopts = FirebaseOptions(contentsOfFile: filePath!) else {
assert(false, "Couldn't load config file")
return
}
FirebaseApp.configure(options: fileopts)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// FirebaseApp.configure()
}
如果您尝试执行上述操作,您可能会注意到一个类似于
[AppDelegateSwizzler] App Delegate does not conform to UIApplicationDelegate protocol firebase
解决方案
你应该做什么:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
var firebasePlistFileName = "GoogleService-Info"
if buildFor == "PROD" {
//firebasePlistFileName = "GoogleService-Info-Production"
firebasePlistFileName = "RELEASE-NEW-GoogleService-Info"
print("--- USING PRODUCTION / RELEASE PLIST")
}
else {
firebasePlistFileName = "DEBUG-NEW-GoogleService-Info"
print("--- USING DEBUG PLIST")
}
let filePath = Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist")
guard let fileopts = FirebaseOptions(contentsOfFile: filePath!) else {
assert(false, "Couldn't load config file")
}
FirebaseApp.configure(options: fileopts)
}
//....
这是假设您已进入方案,已编辑方案以具有环境变量 BUILD_FOR 并将其设置为 PROD 用于您的生产/发布方案,然后将基于此指向正确的 GoogleServices-Info plist 文件名。这是使用 Firebase 实现多个 plist 而不搞砸一切的一种方法。
要点 - 不要使用 override init 除非你想自己修复 swizzling 或者你比我更了解你在做什么。
推荐阅读
- openvpn - 无法下载和安装 .ovpn 文件并将其复制到 MAC 上 OpenVpn 的配置位置
- sql - 计算支付日期
- c - 将位转换为人类可读的格式
- ruby - `block_given?` 是否呈现 `&block` 参数可选?
- ruby-on-rails - 无法解决对 ROR 的依赖问题
- python - Python Pip Install Wheel 将文件放在哪里
- ios - 如何使整个 UIView 淡入白色并返回?
- c# - microsoft.office.interop.powerpoint v16 (Office 365) 与 C#
- javascript - 如何为多个移动屏幕高度(Angular 6)设置垫卡高度?
- python - 如何进行numpy数组颜色相似度转换