iOS无法通过FCM接收推送通知(可以通过APN)

时间:2020-08-14 17:27:51

标签: ios swift firebase firebase-cloud-messaging swift5

我正要在stackoverflow上问这个问题,但是我只是想通了。无论如何,我还是为了后代而发布此消息,因为此错误使我花了几天的时间,而且在该网站上找不到任何提及。

做出两种不同的方案并根据AppDelegate中的方案选择了不同的plist之后,我无法通过FCM发送推送通知。选择我的plist的代码如下:

from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan

query = {
    "query": {"match_all" {}}
}

es = Elasticsearch(...)
for hit in scan(es, index="my-index", query=query):
    print(hit["_source"]["field"])

如果您尝试执行上述操作,则可能会发现类似

的错误
@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()
}

1 个答案:

答案 0 :(得分:0)

您应该做什么:

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实施多个插件而不用搞砸一切的一种方法。

要点-除非您想自行解决问题,或者您比我更了解自己在做什么,否则请不要使用override init。