swift - Firebase InstanceID.instanceID().token() 方法已弃用

标签 swift firebase firebase-cloud-messaging token

我正在使用 swift 和 firebase。以前我使用以下方法获取 firebase token ,然后我用它存储到数据库中以发送通知。

InstanceID.instanceID().token()

自从我更新了我的 firebase 后,此方法显示为已弃用。

'token()' is deprecated: Use instanceIDWithHandler: instead.

我不知道如何使用 instanceIDWithHandler 我尝试了以下但不知道如何获取 token 。

func instanceID(handler: @escaping InstanceIDResultHandler){

    }

请帮忙。 提前谢谢你。

最佳答案

Fetching the current registration token

Registration tokens are delivered via the method messaging:didReceiveRegistrationToken:. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:

  • If the registration token is new, send it to your application server.
  • 将注册 token 订阅到主题。这仅适用于新订阅或用户重新安装应用的情况。

You can retrieve the token directly using instanceIDWithHandler:. This callback provides an InstanceIDResult, which contains the token. A non null error is provided if the InstanceID retrieval failed in any way.

您应该导入 FirebaseInstanceID

  import FirebaseInstanceID

objective-c

在你的 getTokenMethod 上

[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error fetching remote instance ID: %@", error);
    } else {
        NSLog(@"Remote instance ID token: %@", result.token);
    }
}];

swift

InstanceID.instanceID().instanceID { result, error in
    if let error = error {
        print("Error fetching remote instange ID: \(error)")
    } else if let result = result {
        print("Remote instance ID token: \(result.token)")
    }
}

更新

InstanceID 现已弃用。尝试

Messaging.messaging().token { token, error in
   // Check for error. Otherwise do what you will with token here
}

关于swift - Firebase InstanceID.instanceID().token() 方法已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50945015/

相关文章:

ios - 如何在 swift 中使用 api 通用管理器类删除传递数组作为参数的 json 解析错误?

iOS swift : Firebase Dynamic Links: Short URL not working

android - 如果用户拥有多个设备,如何在我的服务器上存储 firebase token

ios - 通过 Cocoapods 包含 Xcode 7 UI 测试依赖项?

swift 。 Facebook 图片 url 返回 nil

Android 依赖项 'android.arch.lifecycle:runtime' 的编译 (1.0.0) 和运行时 (1.1.1) 类路径有不同的版本

javascript - 函数只在第一次点击时运行?

ios - 单击网站链接(Firebase 动态链接)时如何打开应用程序

spring - 如何使用 Spring Boot 发送通知 (FCM)

ios - FCM-如何区分它是在用户单击通知时从 Firebase 控制台发送还是从 HTTP 发送