Firebase推送通知无法在手机上运行但可在模拟器中运行

时间:2016-06-25 08:36:58

标签: 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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

有任何更新吗?

我在其他论坛中找到了这个解决方案。如果您使用Firebase,则必须添加:

使用FirebaseAppDelegateProxyEnabled: NO

<强> TestFlight:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}

<强>生产:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
}

FirebaseAppDelegateProxyEnabled: YES ......理论上这不是必要的。