首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防火墙推送通知iOS问题,未能获取APNS令牌错误Domain=com.firebase.iid Code=1001 "(null)“

防火墙推送通知iOS问题,未能获取APNS令牌错误Domain=com.firebase.iid Code=1001 "(null)“
EN

Stack Overflow用户
提问于 2016-10-20 10:28:24
回答 1查看 1.3K关注 0票数 0

我试图为iOS实现Firebase推送通知,获得此msg:未能获取APNS令牌错误Domain=com.firebase.iid Code=1001 "(null)"

我用的是Swift 2.3Xcode 8

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

一件奇怪的事情是,如果我不启用Xcode App >功能中的Push Notification,我就不会得到错误,它会打印用户信息和令牌:(请帮助。

下面是我的AppDelegate代码和输出

代码语言:javascript
运行
复制
@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.
EN

回答 1

Stack Overflow用户

发布于 2016-11-16 15:48:33

尝试将Firebase/Core更新到v3.4.4,它为我修复了意外错误。此外,避免调用unregisterForRemoteNotifications。在此调用之后,您不能再注册设备来推送通知。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40151854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档