繁体   English   中英

Firebase推送通知iOS问题,无法获取APNS令牌错误Domain = com.firebase.iid代码= 1001“(null)”

[英]Firebase Push Notification iOS issue, Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 “(null)”

我正在尝试为iOS实现Firebase推送通知,得到此消息: 无法获取APNS令牌错误Domain = com.firebase.iid代码= 1001“(null)”

我正在使用Swift 2.3Xcode 8

我已经在Firebase控制台中设置了APNs SSL证书,还已在Xcode App>功能中启用了推送通知,并在BackgroundMode中选中了标记为Remote Notification选项。 还在Info.plist添加了FirebaseAppDelegateProxyEnabled = No ,并在GoogleService-Info.plist添加了正确的Bundle ID

发生一件奇怪的事情,如果我没有在Xcode App>功能中启用推送通知,我不会收到错误,它会打印用户信息和令牌:(请帮助。

下面是我的AppDelegate代码和输出

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //for Firebase
       // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
            authOptions,
            completionHandler: {_,_ in })
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    // [END register_for_notifications]

    FIRApp.configure()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(self.tokenRefreshNotification),
                                                     name: kFIRInstanceIDTokenRefreshNotification,
                                                     object: nil)

    return true
}


// [START receive_message]
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 comes here")
    //print("Message ID: \(userInfo["gcm.message_id"]!)")

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

}
// [END receive_message]

// [START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }

    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}
// [END refresh_token]

// [START connect_to_fcm]
func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}
// [END connect_to_fcm]

func applicationDidBecomeActive(application: UIApplication) {
    connectToFcm()
}

// [START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
    FIRMessaging.messaging().disconnect()
    print("Disconnected from FCM.")
}
// [END disconnect_from_fcm]

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

   // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(center: UNUserNotificationCenter,
                                willPresentNotification notification: UNNotification,
                                                       withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
       // Print message ID.
     //   print("Message ID: \(userInfo["gcm.message_id"]!)")

        // 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]


--------OUTPUT
2016-10-20 15:50:03.051 ProgrammingHub[2747:916824] WARNING: Firebase Analytics App Delegate Proxy is disabled. To log deep link campaigns manually, call the methods in FIRAnalytics+AppDelegate.h.

2016-10-20 15:50:04.073 ProgrammingHub[2747:916824] [Crashlytics] Version 3.7.2 (112)

2016-10-20 15:50:04.287 ProgrammingHub[2747] <Debug> [Firebase/Core][I-COR000001] Configuring the default app.

2016-10-20 15:50:04.314: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"

2016-10-20 15:50:04.326: <FIRMessaging/INFO> FIRMessaging library version 1.2.0

2016-10-20 15:50:04.380 ProgrammingHub[2747:] <FIRAnalytics/INFO> Firebase Analytics v.3404000 started

2016-10-20 15:50:04.382 ProgrammingHub[2747:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http:/goo.gl/Y0Yjwu)

2016-10-20 15:50:04.692 ProgrammingHub[2747:916824] INFO: GoogleAnalytics 3.11 -[GAIReachabilityChecker reachabilityFlagsChanged:] (GAIReachabilityChecker.m:159): Reachability flags update: 0X000002

2016-10-20 15:50:04.988 ProgrammingHub[2747:916824] INFO: GoogleAnalytics 3.11 -[GAIReachabilityChecker reachabilityFlagsChanged:] (GAIReachabilityChecker.m:159): Reachability flags update: 0X000002

2016-10-20 15:50:05.208 ProgrammingHub[2747:] <FIRAnalytics/INFO> Firebase Analytics enabled

2016-10-20 15:50:05.391 ProgrammingHub[2747:916926] INFO: GoogleAnalytics 3.11 -[GAIBatchingDispatcher hitsForDispatch] (GAIBatchingDispatcher.m:368): No pending hits.
2016-10-20 15:50:06.166 ProgrammingHub[2747] <Debug> [Firebase/Core][I-COR000019] Clearcut post completed.

尝试将Firebase / Core更新到v3.4.4,它为我修复了意外错误。 另外,避免调用unregisterForRemoteNotifications 通话结束后,您将无法再注册该设备以推送通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM