繁体   English   中英

APNS Firebase通知无法获取令牌

[英]APNS Firebase Notification failed to fetch token

对于Swift3 / iOS10,请参见以下链接:

ios10,Swift 3和Firebase推送通知(FCM)

我正在尝试使用Firebase进行通知,并且完全按照文档中的说明进行了集成。 但是我不明白为什么它不起作用。 在构建项目时,我看到以下行:

2016-05-25 16:09:34.987: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"

这是我的AppDelegate:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
     var service: DataService = DataService()
    service.start()
    registerForPushNotifications(application)
    application.registerForRemoteNotifications()
    return true
}

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
    print("Device Token:", tokenString)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)  {
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}

我也遇到了同样的问题,对我没有任何帮助。 但是,您要做的就是转到您的Firebase控制台,然后找到您的项目并转到其设置,在此处检查其云消息传递选项卡,然后将.p12证书上传到其中。

而已! 快乐的编码:)

1.在didFinishLaunchingWithOptions方法中设置Notification Observer

2.然后设置tokenRefreshNotification方法,然后在此方法中获取令牌。

见下面的代码

import Firebase
import FirebaseMessaging

override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  FIRApp.configure()

      NotificationCenter.default.addObserver(self,
                                                     selector: #selector(self.tokenRefreshNotification(notification:)),
                                                     name: NSNotification.Name.firInstanceIDTokenRefresh,
                                                     object: nil)
}

// NOTE: Need to use this when swizzling is disabled
public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

  FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}

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.")
    }
  }
}

public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  print(userInfo)
}

1-您是否已按照google文档中的说明正确配置了证书(我在这里不会回忆起这个过程,因为它很长...)? https://firebase.google.com/docs/cloud-messaging/ios/certs#configure_an_app_id_for_push_notifications

2-设置FCM时遇到了一些困难。 一旦我认为一切正常,但是通知仍然无法正常工作,我决定从手机中完全删除该应用程序,清理构建文件夹,然后重新安装整个程序。 在那之后,它开始工作了。

3-该应用程序正在接收通知,但是我仍然收到“无法获取默认令牌...”消息。 过了一会儿,它消失了。 不要问我为什么!

这并不是一个正确的答案,我只是分享我的经验,因为我知道配置通知并不容易,并且欢迎所有提示。 所以也许这个可以帮上忙。 干杯:)

在尝试了上述所有方法(以及在其他地方可以找到的所有方法)之后,为我解决问题的是移动

let token = FIRInstanceID.instanceID().token()

在按下按钮时调用,而不是在应用加载时调用。

我知道这可能不是最优雅的解决方案,但是对于调试而言已经足够了。 我猜想令牌不能立即由服务器使用,并且需要一些时间来生成。

FCM为我工作,然后停了下来。 我按照Rabs G.的建议做了,删除了该应用程序并再次安装,并且通知再次开始工作。

上面的答案涵盖了大多数问题,但是我遇到了同样的问题,并且发现以下信息很有用:

  1. Firebase可以随时“旋转” (更改)用户的FCM令牌。 这是您的服务器用来将推送通知发送到设备的128个字符的ID。

  2. Firebase文档说,最佳实践是使用委托通过委托回调方法监视更改:

     - (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken 

[Obj-C]

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)

[迅速]

每次更改都应调用委托方法,此时您可以更新服务器中的记录。

  1. 不幸的是,这对我不起作用,我有一个委托,但未调用回调。 因此,我不得不在每次启动应用程序时手动更新令牌(如上述@micheal chein所建议),如下所示:

     NSString *deviceToken = [FIRInstanceID instanceID].token; // Send this to your server 

    [Obj-C]

     let token = FIRInstanceID.instanceID().token() // Send this to your server 

    [迅速]

**重要提示:延迟(20-25s)后更新令牌,因为轮换有时只能在一段时间后反映出来。 您可以为此使用计时器。

  1. 之后,我仍然收到APNS警告/错误消息:

     2017-06-06 09:21:49.520: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)" 

但是 ,推送通知每次都会正常运行。 因此,我认为该日志消息有点过时(可能是时机错误)。 如果您可以选择第二种方法来工作,那么绝对可以!

暂无
暂无

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

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