Firebase FCM推送通知已停止在iOS 11.1.1中运行 [英] Firebase FCM push notifications stopped working iOS 11.1.1

查看:176
本文介绍了Firebase FCM推送通知已停止在iOS 11.1.1中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase FCM从iOS设备发送推送通知。推送通知确实有效,直到昨天。



当我现在发送推送通知时,一切都显示成功,但设备上没有收到任何内容。



如果我直接通过curl请求发送,这是回复:

  {multicast_id :7815294000653973158,成功:1,失败:0,canonical_ids:0,结果:[{message_id:0:1510474219556035%ca9ff0f8ca9ff0f8}]} 

如果我从firebase控制台上的通知仪表板发送,则显示已成功完成。



我做了以下操作但没有成功:


  1. 锁定在FirebaseInstanceID,2.0.0的pod中,按照



    我的Info.plist:



    如果需要任何其他信息,请与我们联系。



    我的Android应用程序仍然像以前一样工作。我很难理解在一天内iOS应用程序可能会发生什么变化导致这种情况,或者我可能在一天内破坏的情况:)



    任何帮助将不胜感激。

    解决方案

    好的,修好了。我将firebase的消息添加到我的Info.plist中禁用了firebase的消息:FirebaseAppDelegateProxyEnabled:NO



    此外,我删除了所有firebase消息传递委托方法。并且在didRegisterForRemoteNotificationsWithDeviceToken中生成我自己的APN令牌,并在生成令牌后在didRegisterForRemoteNotificationsWithDeviceToken方法中设置Messaging.messaging()。apnsToken = deviceToken。

      func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
    if let refreshedToken = InstanceID.instanceID()。token(){
    defaults.set(refreshedToken,forKey:Constant.UserDefaults.token )

    Messaging.messaging()。apnsToken = deviceToken

    print(Token generated:,refreshedToken)
    } else {
    print(无法使用实例ID令牌保存令牌becuase错误)
    }
    }

    之前我使用了firebase调色,似乎因某种原因停止了工作。



    因为这对我来说是一次艰难的经历,所以我想发布这些步骤我现在会recco修改为FCM启用iOS客户端:


    1. 在Apple Developer控制台中生成您的APN,然后选择APN。选择继续并下载您的APN证书。记下您的密钥ID。



    然后在firebase控制台中根据设置,云消息传递,上传您的APN密钥,添加密钥ID和捆绑ID,不要上传任何p12证书。




    1. 通过添加此消息来禁用firebase消息调配您的Info.plist:




    FirebaseAppDelegateProxyEnabled:NO





    1. 然后在didFinishLaunchingWithOptions方法的AppDelegate.swift文件中添加以下内容:



      func应用程序(_ application:UIApplication,didFinishLaunchingWit hOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {

        // [START set_messaging_delegate] 
      Messaging。 messaging()。delegate = self
      // [END set_messaging_delegate]

      //获取用户
      的推送通知令牌ID如果#available(iOS 10.0,*){
      //适用于iOS 10显示通知(通过APNS发送)
      UNUserNotificationCenter.current()。delegate = self

      let authOptions:UNAuthorizationOptions = [.alert,.badge,.sound]
      UNUserNotificationCenter.current()。requestAuthorization(
      options:authOptions,
      completionHandler:{_,_ in})
      } else {
      let settings:UIUserNotificationSettings =
      UIUserNotificationSettings(类型:[.alert,.badge,.sound],类别:nil)
      application.registerUserNotificationSettings(settings)
      }

      application.registerForRemoteNotifications()

      返回true

      }


    由于调配被禁用,您需要注意生成APN令牌并将其分配给firebase消息传递apnsToken。您可以通过在AppDelegate.swift文件中使用以下方法来执行此操作:

      func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:数据){
    如果让refreshedToken = InstanceID.instanceID()。token(){
    defaults.set(refreshedToken,forKey:Constant.UserDefaults.token)

    Messaging.messaging ()。apnsToken = deviceToken

    print(Token generated:,refreshedToken)
    } else {
    print(无法保存令牌,因为实例ID令牌)
    }
    }

    然后我将令牌保存在userdefaults中以便以后保留到数据库见上文:


    defaults.set(refreshedToken,forKey:Constant.UserDefaults.token)


    您可以通过复制在控制台中打印出的令牌并使用FCM消息传递控制台来测试您的令牌是否正常工作。或者通过在终端中使用卷曲请求,如下所示。请注意,您必须将yOUR_LEGACY_SERVER_KEY替换为您可以在firebase控制台中根据设置和云消息传递找到的旧服务器密钥,并将YOUR_TOKEN替换为控制台中打印的令牌(上面生成的):

      curl -XPOSThttps://fcm.googleapis.com/fcm/send\ 
    -H授权:key = YOUR_LEGACY_SERVER_KEY \
    -H内容类型:application / json\
    -d $'{
    notification:{
    body:直接测试FCM API,
    title:测试消息,
    徽章:0,
    声音:默认
    },
    registration_ids:[YOUR_TOKEN]
    }'


    I am using Firebase FCM to send push notifications from an iOS device. The push notifications did work, until yesterday.

    When I now send a push notification, everything shows successful, but nothing is received on the device.

    If I send directly via a curl request, this is the response:

    {"multicast_id":7815294000653973158,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1510474219556035%ca9ff0f8ca9ff0f8"}]}
    

    If I send from the Notification dashboard on the firebase console, it shows successfully completed.

    I have done the following with no success:

    1. Locked in pod for 'FirebaseInstanceID', "2.0.0", as per FCM Push notifications do not work on iOS 11
    2. Generated a new APN key on developer console and replaced the existing key on FCM setup in Firebase
    3. Downloaded a fresh GoogleService-Info.plist and replaced existing
    4. Checked that bundle id's etc all match
    5. Updated firebase pods to latest:

    Using Firebase (4.5.0) Using FirebaseAnalytics (4.0.4) Using FirebaseAuth (4.3.1) Using FirebaseCore (4.0.10) Using FirebaseFirestore (0.9.1) Using FirebaseInstanceID (2.0.5) Using FirebaseMessaging (2.0.6)

    1. Turned message swizzling on and off
    2. Setting Messaging.messaging().shouldEstablishDirectChannel = true as per Firebase notifications not working in iOS 11
    3. Deleted this iOS app from firebase console and redone notification setup from the start
    4. Made sure remote notification in Capabilities are still turned on
    5. Restarted my devices

    My setup: I call FirebaseApp.configure() in the AppDelegate file in the override init() method:

      override init() {
        super.init()
        FirebaseApp.configure()
      }
    

    In the AppDelegate didFinishLaunchingWithOptions:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        // get push notification token id for user
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
    
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
    
        application.registerForRemoteNotifications()
    
        return true
    }
    

    And then I save my token in userdefaults and persist to database later:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        if let refreshedToken = InstanceID.instanceID().token() {
            defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
            print("Token generated: ", refreshedToken)
        } else {
            print("Could not save token becuase error with instance id token")
        }
    }
    

    The token generated here I also test with curl as mentioned above and all shows successful.

    My GoogleService-Info.plist:

    My Info.plist:

    Please let me know if any other info is required.

    My Android application is still working as before. I am struggling to understand what could change for the iOS app in one day to cause this, or what I perhaps broke in one day:)

    Any help would be greatly appreciated.

    解决方案

    Ok, fixed it. I disable firebase's message swizzling by adding this to my Info.plist: FirebaseAppDelegateProxyEnabled: NO

    Further I removed all firebase messaging delegate methods. And generated my own APN token in didRegisterForRemoteNotificationsWithDeviceToken and setting Messaging.messaging().apnsToken = deviceToken in the didRegisterForRemoteNotificationsWithDeviceToken method once token generated.

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        if let refreshedToken = InstanceID.instanceID().token() {
            defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
    
            Messaging.messaging().apnsToken = deviceToken
    
            print("Token generated: ", refreshedToken)
        } else {
            print("Could not save token becuase error with instance id token")
        }
    }
    

    Previously I used the firebase swizzling, it appears as if this stopped working for some reason.

    As this was a tough experience for me, I would like to post the steps that I would now reccommend to enable iOS client for FCM:

    1. In the Apple Developer console generate your APN and then select APNs. Select Continue and download your APN certificate. Take note of your Key ID.

    Then in the firebase console under settings, cloud messaging, upload your APN key, add the key ID and bundle id, do not upload any p12 certificates.

    1. Disable firebase message swizzling by adding this to your Info.plist:

    FirebaseAppDelegateProxyEnabled: NO

    1. Then add the following in your AppDelegate.swift file in the didFinishLaunchingWithOptions method:

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

      // [START set_messaging_delegate]
      Messaging.messaging().delegate = self
      // [END set_messaging_delegate]
      
      // get push notification token id for user
      if #available(iOS 10.0, *) {
          // For iOS 10 display notification (sent via APNS)
          UNUserNotificationCenter.current().delegate = self
      
          let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
          UNUserNotificationCenter.current().requestAuthorization(
              options: authOptions,
              completionHandler: {_, _ in })
      } else {
          let settings: UIUserNotificationSettings =
              UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
          application.registerUserNotificationSettings(settings)
      }
      
      application.registerForRemoteNotifications()
      
      return true
      

      }

    Since swizzling is disabled you need to take care of generating your APN token and assigning it to the firebase messaging apnsToken. You do this by having the following method in your AppDelegate.swift file:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        if let refreshedToken = InstanceID.instanceID().token() {
            defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
    
            Messaging.messaging().apnsToken = deviceToken
    
            print("Token generated: ", refreshedToken)
        } else {
            print("Could not save token becuase error with instance id token")
        }
    }
    

    I then save the token in the userdefaults to later persist to the database see above:

    defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)

    You can test that your token is working by copying the token printed out in the console and using the FCM messaging console. Or by using a curl request in the terminal as below. Note you have to replace YOUR_LEGACY_SERVER_KEY with the legacy server key which you can find in the firebase console under settings and cloud messaging and replace YOUR_TOKEN with the token printed in the console (which was generated above):

    curl -X "POST" "https://fcm.googleapis.com/fcm/send" \
     -H "Authorization: key=YOUR_LEGACY_SERVER_KEY" \
     -H "Content-Type: application/json" \
     -d $'{
    "notification": {
    "body": "Testing with direct FCM API",
    "title": "Test Message",
    "badge": "0",
    "sound": "default"
    },
    "registration_ids": [YOUR_TOKEN]
    }'
    

    这篇关于Firebase FCM推送通知已停止在iOS 11.1.1中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆