3

我正在尝试让我的远程推送通知在我的手机上工作。当我将手机连接到计算机并通过手机运行模拟器时,从 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)
    }
}

在此处输入图像描述

4

2 回答 2

1

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

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

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

于 2016-07-02T05:37:18.707 回答
0

任何更新?

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

FirebaseAppDelegateProxyEnabled: NO

试飞:

-(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...理论上这是没有必要的..

于 2017-01-17T02:05:49.100 回答