Ios Firebase无法获取APNS令牌错误域=com.Firebase.iid Code=1001";(无效)“;
我的猜测是,如果没有最后两个方法,它应该可以工作,但我还是把它们放在了那里(根据print语句,看起来它们没有被调用) 我将注册信息称为:Ios Firebase无法获取APNS令牌错误域=com.Firebase.iid Code=1001";(无效)“;,ios,firebase,firebase-cloud-messaging,Ios,Firebase,Firebase Cloud Messaging,我的猜测是,如果没有最后两个方法,它应该可以工作,但我还是把它们放在了那里(根据print语句,看起来它们没有被调用) 我将注册信息称为: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after app
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
....
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("DEVICE TOKEN = \(deviceToken)")
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
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
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
func tokenRefreshNotification(notification: NSNotification) {
// NOTE: It can be nil here
let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
我可以看到控制台上打印的deviceToken。但我还是有火基问题。我还可以检查其他方面的想法吗?我曾尝试构建基于Firebase/Quickstart iOS的简单应用程序iOS示例(Swift),但我遇到了相同的问题:
无法获取APNS令牌Error Domain=com.Firebase.iid code=1001”(null)
应用程序处于后台时,我无法接收通知。
所以,我在这里找到了适合我的解决方案:
基本上,我添加了这个方法:
static func registerForNoties(){
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(pushNotificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
。。。这一行代码:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Handle push from background or closed" );
print("%@", response.notification.request.content.userInfo);
}
显然,用Objective-C编写的示例效果很好,但Swift中的示例缺少一些代码,无法按预期工作…尝试将Firebase/Core更新到v3.4.4,它为我修复了意外错误。否则,请尝试避免调用
unregisterForRemoteNotifications
。此呼叫后,您无法再注册设备以推送通知。另外,请尝试在info.plist文件中将FirebaseAppDelegateProxyEnabled
设置为NO
。我认为他们的方法存在漏洞。调用此方法,您将获得设备令牌。我可以看到控制台上打印的deviceToken
application.registerForRemoteNotifications()
你解决了这个问题吗?没有,我最终放弃了Firebase的iOS版,使用了Pyans(我的后端是用django/Python编写的)。你应该在第一个屏幕出现的地方调用这个方法。在appdelegate.add之后在viewdidload()中添加此方法
application.registerForRemoteNotifications()
dispatch_async(dispatch_get_main_queue(),
{
global.appdel.tokenRefreshNotification()
})