簡體   English   中英

iOS 無法通過 FCM 接收推送通知(可以通過 APNs)

[英]iOS cannot receive push notifications through FCM (can through APNs)

我正要在stackoverflow上問這個問題,但我只是想通了。 為了后代,我還是發布了這個,因為這個錯誤花了我幾天的時間,我在這個網站上找不到任何提及它的地方。

在制作了兩種不同的方案並根據 AppDelegate 中的方案選擇不同的 plist 后,我無法通過 FCM 發送推送通知。 select 我的 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 或者你比我更了解你在做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM