iOS 无法通过 FCM 接收推送通知(可以通过 APNs)
Posted
技术标签:
【中文标题】iOS 无法通过 FCM 接收推送通知(可以通过 APNs)【英文标题】:iOS cannot receive push notifications through FCM (can through APNs) 【发布时间】:2020-08-14 17:27:51 【问题描述】:我正要在***上问这个问题,但我刚刚想通了。为了后代,我还是发布了这个,因为这个错误花了我几天的时间,我在这个网站上找不到任何提及它。
根据 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
【问题讨论】:
【参考方案1】:你应该做什么:
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 或者你比我更了解你在做什么。
【讨论】:
以上是关于iOS 无法通过 FCM 接收推送通知(可以通过 APNs)的主要内容,如果未能解决你的问题,请参考以下文章