【问题标题】:Push Notifications not sending from FCM after APNs Cert changeAPNs 证书更改后未从 FCM 发送推送通知
【发布时间】:2017-09-25 18:31:02
【问题描述】:

在我的一个证书过期之前,我从 FCM 向我的 iOS 应用程序发送推送通知没有任何问题。更改后,FCM 不再传递消息。我阅读了这篇文章 (https://firebase.googleblog.com/2017/01/debugging-firebase-cloud-messaging-on.html),这里是我到目前为止所经历的验证步骤,但现在我的头在墙上...

  • 注释掉所有 connectToFCM 函数
  • 已下载 Pusher 并成功可以使用 APNs 证书向设备发送通知
  • 成功地对 FCM 进行 curl 调用(响应如下)
    • {"multicast_id":7774794018682407760,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1493321644068397%b76e8527b76e8527"}]}李>
  • 尝试重新创建开发和生产证书
  • 尝试使用和不使用密码从钥匙串中导出它们

任何人都经历过这种超级令人沮丧的事情,并有关于如何进行的建议?

还值得注意的是,我无法删除 APNs 证书,我看到了该选项,但它是灰色的,我无法选择它。

【问题讨论】:

  • 您是否将新证书上传到 Firebase?
  • 是的,我已经上传了多个版本以尝试使其正常工作。
  • 解决方案:我重新安装了应用程序,它可以工作了。

标签: ios firebase apple-push-notifications firebase-cloud-messaging


【解决方案1】:

您可以创建一个 APNs 密钥并将其用于所有应用程序,而不是使用 APNS 证书进行推送通知。

APN 使用 Apple Push Notification 服务处理您的通知请求。一键用于您的所有应用程序。有关详细信息,请参阅本地和远程通知编程指南。

甚至 FCM 也支持这一点。现在您可以避免为每个 App ID 创建 APNS 证书的麻烦了。

【讨论】:

【解决方案2】:

这有助于理解流程。


图片由The Firebase Blog提供

您已测试 APN 发送到应用程序。所以证书没问题。

来自您链接的the blog,通过进行 curl 调用获得成功响应仅意味着 FCM 正在接收该消息。这并不意味着消息正在发送到 APN。

直接使用 Firebase 通知面板发送消息,以查看 FCM 是否正在与 APN 通信。

如果可行,则说明您的消息格式存在问题。

如果它不起作用。

确保:

  1. 邮件优先级设置为high,因此会立即发送。

  2. “content_available”:真

  3. 卸载并重新安装应用程序

  4. 检查您的服务器代码。

【讨论】:

    【解决方案3】:

    请在 YourProjectSetting=>cloudmessaging=> 中更新您的 APNs 证书(生产和开发) iOS 应用程序配置。 Firebase 云消息传递可以使用 APNs 身份验证密钥或 APNs 证书来连接 APNs

    注意:确认您的项目凭据(SenderID、旧服务器密钥、服务器密钥)。

    HTTP POST 请求:用于发送通知。

    https://fcm.googleapis.com/fcm/send
    Content-Type:application/json
    Authorization:key=AIzaSyZ-1u......7Udno5aA
    {
        "registration_ids": ["regId1","regId2"],
        "data": {
            "title": "App Name",
            "message": "Hello this is for testing",
            "body": "Hello this is for testing"
    
        },
        "content-available": true,
        "priority": "high",
        "notification": {
            "title": "App Name",
            "message": "Hello this is for testing",
            "body": "Hello this is for testing"
        }
    }
    

    在您的AppDelegate.swift中添加以下代码

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
                self.fcmInitialSetup(application)
                return true
            }
            func fcmInitialSetup(_ application: UIApplication){
    
                // [START register_for_notifications]
                if #available(iOS 10.0, *) {
                    let uns: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                    application.registerUserNotificationSettings(uns)
                    application.registerForRemoteNotifications()
    
                } else {
                    let settings: UIUserNotificationSettings =
                        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                    application.registerUserNotificationSettings(settings)
                }
    
                application.registerForRemoteNotifications()
    
                // [END register_for_notifications]
    
                FIRApp.configure()
    
                // Add observer for InstanceID token refresh callback.
                NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)
    
                if let token = FIRInstanceID.instanceID().token() {
                    sendTokenToServer(token)
                }
            }
    
            func sendTokenToServer(_ currentToken: String) {
                print("sendTokenToServer() Token: \(currentToken)")
                // Send token to server ONLY IF NECESSARY
    
                print("InstanceID token: \(currentToken)")
                self.token = currentToken
                UserDefaults.standard.set(self.token, forKey: "token")
                UserDefaults.standard.synchronize()
                if self.token != nil{
                    let userInfo = ["token": self.token]
                    NotificationCenter.default.post(
                        name: Notification.Name(rawValue: self.rkey), object: nil, userInfo: userInfo)
                }
            }
    
    
            // NOTE: Need to use this when swizzling is disabled
            func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
                let tokenChars = (deviceToken as NSData).bytes.bindMemory(to: CChar.self, capacity: deviceToken.count)
                var tokenString = ""
    
                for i in 0..<deviceToken.count {
                    tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
                }
    
                FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.unknown)
                print("Device Token:", tokenString)
                print("FIRInstanceID.instanceID().token() Token:", FIRInstanceID.instanceID().token())
                if let tokenData = FIRInstanceID.instanceID().token(){
                    UserDefaults.standard.set(tokenData, forKey: "token")
                    UserDefaults.standard.synchronize()
                    let userInfo = ["token": tokenData]
                }
            }
    
            func tokenRefreshNotification(_ notification: Notification) {
                // NOTE: It can be nil here
                //        print("Token:\(FIRInstanceID.instanceID().token()!)")
                if let refreshedToken = FIRInstanceID.instanceID().token() {
                    print("InstanceID token: \(refreshedToken)")
                    UserDefaults.standard.set(refreshedToken, forKey: "token")
                    UserDefaults.standard.synchronize()
                    print("update now \(self.token)")
                    if self.token != nil{
                        let userInfo = ["token": self.token]
                        NotificationCenter.default.post(
                            name: Notification.Name(rawValue: self.rkey), object: nil, userInfo: userInfo)
                    }
    
                }
    
                // Connect to FCM since connection may have failed when attempted before having a token.
                connectToFcm()
            }
            // [END refresh_token]
            func connectToFcm() {
                FIRMessaging.messaging().connect { (error) in
                    if (error != nil) {
                        print("Unable to connect with FCM. \(error)")
                    } else {
                        print("Connected to FCM.")
                    }
                }
            }
    
            func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
                print(userInfo)
            }
    
            func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
                print("Within open URL")
                return true
            }
    
    
            // [START receive_apns_token_error]
            func application( _ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
                error: Error ) {
                print("Registration for remote notification failed with error: \(error.localizedDescription)")
                // [END receive_apns_token_error]
                let userInfo = ["error": error.localizedDescription]
                NotificationCenter.default.post(
                    name: Notification.Name(rawValue: rkey), object: nil, userInfo: userInfo)
            }
    
            func registrationHandler(_ token: String!, error: NSError!) {
                if (token != nil) {
                    self.token = token!
                    print("Registration Token: \(self.token)")
                    UserDefaults.standard.set(self.token, forKey: "token")
                    UserDefaults.standard.synchronize()
                    let userInfo = ["token": self.token]
                    NotificationCenter.default.post(
                        name: Notification.Name(rawValue: self.rkey), object: nil, userInfo: userInfo)
                } else {
                    print("Registration to GCM failed with error: \(error.localizedDescription)")
                    let userInfo = ["error": error.localizedDescription]
                    NotificationCenter.default.post(
                        name: Notification.Name(rawValue: self.rkey), object: nil, userInfo: userInfo)
                }
            }
    
            func registerForPushNotifications(_ application: UIApplication) {
                let notificationSettings = UIUserNotificationSettings(
                    types: [.badge, .sound, .alert], categories: nil)
                application.registerUserNotificationSettings(notificationSettings)
            }
    
            func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
                if notificationSettings.types != UIUserNotificationType() {
                    application.registerForRemoteNotifications()
                }
            }
    
            // [START receive_message]
            func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                             fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
                // If you are receiving a notification message while your app is in the background,
                // this callback will not be fired till the user taps on the notification launching the application.
                // TODO: Handle data of notification
    
                // Print message ID. add Toast
                print(userInfo);
    
                print(application.keyWindow?.visibleViewController() ?? "")
    
                print("Message ID: \(userInfo["gcm.message_id"]!)")
    
    
                // Print full message.
                print("%@", userInfo)
            }
            // [END receive_message]
    
    
    
            func applicationDidBecomeActive(_ application: UIApplication) {
                connectToFcm()
            }
    
            // [START disconnect_from_fcm]
            func applicationDidEnterBackground(_ application: UIApplication) {
                //        FIRMessaging.messaging().disconnect()
                //        print("Disconnected from FCM.")
            }
    
            func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
            }
    
            // [END disconnect_from_fcm]
    
    
    // [START ios_10_message_handling]
    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
        // Receive displayed notifications for iOS 10 devices.
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo
            // Print message ID.
            print("Message ID: \(userInfo["gcm.message_id"]!)")
            // Print message ID. add Toast
    
            // Print full message.
            print("%@", userInfo)
            // Print full message.
            print("%@", userInfo)
        }
    }
    
    extension AppDelegate : FIRMessagingDelegate {
        // Receive data message on iOS 10 devices.
        func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
            print("%@", remoteMessage.appData)
        }
    }
    
    // [END ios_10_message_handling]
    

    参考:https://firebase.google.com/docs/cloud-messaging/ios/device-group

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多