ios - Firebase 推送通知不适用于手机但适用于模拟器

标签 ios swift firebase firebase-notifications

我正在尝试让我的远程推送通知在我的手机上运行。当我将手机连接到计算机并通过手机运行模拟器时,从 Firebase 控制台发送的通知工作得很好。但是,当我将它上传到 Testflight 并将其下载到我的手机上时,我的推送通知没有通过。

我的证书已正确上传,下面是我的代码

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        FIRApp.configure()
        FIRDatabase.database().persistenceEnabled = true
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let notificationTypes : UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        application.registerForRemoteNotifications()
        application.registerUserNotificationSettings(notificationSettings)

        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func application(application: UIApplication,
        openURL url: NSURL,
        sourceApplication: String?,
        annotation: AnyObject) -> Bool {
            return FBSDKApplicationDelegate.sharedInstance().application(
                application,
                openURL: url,
                sourceApplication: sourceApplication,
                annotation: annotation)
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        FBSDKAppEvents.activateApp()
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                     fetchCompletionHandler completionHandler: (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.
        print("Message ID: \(userInfo["gcm.message_id"]!)")

        // Print full message.
        print("%@", userInfo)

        completionHandler(.NoData)
    }


    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error)
        print(error.description)
    }
}

enter image description here

最佳答案

您是否创建了 APNs 分发证书并提交到 Firebase 控制台?

请查看本指南: https://firebase.google.com/docs/cloud-messaging/ios/certs#configure_an_app_id_for_push_notifications

“该应用现已启用使用推送通知开发环境。当您准备好发布您的应用时,您需要启用该应用以使用推送通知生产环境:重复这些步骤,但单击创建Production SSL Certificate 部分下的证书而不是 Development SSL Certificate。 如果您正在调用 setAPNSToken,请确保类型的值已正确设置:沙箱环境为 FIRInstanceIDAPNSTokenTypeSandbox,生产环境为 FIRInstanceIDAPNSTokenTypeProd。如果您没有设置正确的类型,消息将不会传送到您的应用。”

关于ios - Firebase 推送通知不适用于手机但适用于模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38026595/

相关文章:

android - Flutter AppUpdateService 链接到死亡

ios - 应用程序因 iOS 13.2 中的 CPU 使用而终止

iphone - 在导航 View Controller 中加载特定场景

ios - 用于共享 vCard 的 UIActivity

java - 使用 FirebaseUI 在 Android TextView 中绑定(bind)嵌套的 JSON

firebase - 遍历 Firestore 集合求和值的文档的最佳方法是什么?

iOS UINavigationBar 色调颜色看起来比颜色集暗

swift - 如何限制两条锚定线的运动,使它们像钟摆一样不断摆动

swift - 当我 append 一个值时,整个数组都会更改为 append 值

android - react native 谷歌登录开发人员错误代码 10