ios - 推送通知在 IOS 11 中不起作用

标签 ios swift firebase push-notification

您好,我正在尝试实现 google firebase 推送通知。

我使用的是 IOS 11Xcode 9.0

Pod 文件

target 'VQ Smart Home' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for VQ Smart Home
        pod 'SwiftyJSON'
        pod 'NVActivityIndicatorView'
        pod 'FirebaseInstanceID', '2.0.0'
        pod 'Firebase/Core'
        pod 'Firebase/Messaging'
end

应用程序委托(delegate)

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

        UINavigationBar.appearance().isTranslucent = true

    /************************ FireBase Notification ************************************************************************/
        FirebaseApp.configure()
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            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()

        let token = Messaging.messaging().fcmToken
        print("*********************************************************************************************************************")
        print("FCM token: \(token ?? "")")
        print("*********************************************************************************************************************")
    /************************ FireBase Notification ************************************************************************/

        return true
    }

 // Push Notification Methods
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }

错误

*********************************************************************************************************************
FCM token: dEukPynDndQ:APA91bG41IAmwarrpdqoUmYxQQ_a6Ro-13YycSXTTyrqRHDGnroJ0pI2cSS5XYIQJwL50s07zUJcaujuj7SY-F5jO_bVEkVpFFyz4jylmwAlgief65Cpl6_TSgNNXkd-eDse7p1uk_rE
*********************************************************************************************************************
- [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension length is less than sct data length
- [MC] Lazy loading NSBundle MobileCoreServices.framework
- [MC] Loaded MobileCoreServices.framework
- [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
- refreshPreferences: HangTracerEnabled: 0
- refreshPreferences: HangTracerDuration: 500
- refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0
- [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40004000 started
- [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (URL)
- TIC Read Status [1:0x0]: 1:57
- TIC Read Status [1:0x0]: 1:57

我已将正确的 APNs key 添加到 Firebase。并且仍然没有收到推送通知。有人可以帮我解决这个问题吗?交易。

最佳答案

找到解决办法

更新 podfile

pod 'Firebase/Core', '4.0.4'
pod 'Firebase/Database', '4.0.4'
pod 'Firebase/Messaging', '4.0.4'
pod 'FirebaseInstanceID', '2.0.0'

添加此方法didRegisterForRemoteNotificationsWithDeviceToken

应用程序委托(delegate)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }

现在尝试通过 firebase Ui 发送推送通知,您将在此方法中收到它 (didReceiveRemoteNotification)

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        let aps = userInfo["aps"] as! NSDictionary
        print(aps["alert"]  as? String ?? "")
        completionHandler(UIBackgroundFetchResult.newData)
    }

关于ios - 推送通知在 IOS 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149238/

相关文章:

ios - Apple 的 "No cancellation of the current subscription is allowed during active subscription period."是什么意思?

ios - 检查DISPATCH_QUEUE_PRIORITY_BACKGROUND是否存在的最佳实践?

swift - <<错误类型>>奇怪的错误

javascript - 使用nodeJs和firestore数据库添加数据而不是替换它

firebase - 无法在 iOS 9.2 上接收 FCM 主题消息

swift - Firebase 独特值(value)

ios - UIButton 文本和图像

ios - 如果只支持iOS6及以上,是否可以消除iPhone非视网膜图像?

ios - 快速 NSPredicate 比较字符串

ios - 视频无法使用 swift 代码