Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Firebase InstanceID.InstanceID().token()方法已弃用_Swift_Firebase_Firebase Cloud Messaging_Token - Fatal编程技术网

Swift Firebase InstanceID.InstanceID().token()方法已弃用

Swift Firebase InstanceID.InstanceID().token()方法已弃用,swift,firebase,firebase-cloud-messaging,token,Swift,Firebase,Firebase Cloud Messaging,Token,我正在和斯威夫特和firebase合作。之前,我使用以下方法获取firebase令牌,然后将其存储到数据库中以发送通知 InstanceID.instanceID().token() 现在,由于我更新了firebase,此方法显示为已弃用 'token()' is deprecated: Use instanceIDWithHandler: instead. 我不知道如何使用instanceIDWithHandler我尝试了以下操作,但不知道如何获取令牌 func instanceID(ha

我正在和斯威夫特和firebase合作。之前,我使用以下方法获取firebase令牌,然后将其存储到数据库中以发送通知

InstanceID.instanceID().token()
现在,由于我更新了firebase,此方法显示为已弃用

'token()' is deprecated: Use instanceIDWithHandler: instead.
我不知道如何使用
instanceIDWithHandler
我尝试了以下操作,但不知道如何获取令牌

func instanceID(handler: @escaping InstanceIDResultHandler){

    }
请帮忙。 提前谢谢你。

注册令牌通过方法
消息传递:didReceiveRegistrationToken:
传递。此方法通常在每个应用程序启动时使用FCM令牌调用一次。调用此方法时,最好是:

  • 如果注册令牌是新的,请将其发送到应用程序服务器
  • 向主题订阅注册令牌。这仅适用于新订阅或用户重新安装应用程序的情况
您可以使用instanceIDWithHandler:直接检索令牌。此回调提供了一个包含令牌的InstancedResult。如果InstanceID检索以任何方式失败,则会提供非空错误

您应该导入FirebaseInstanceID

  import FirebaseInstanceID
目标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
}

请参见

您是在询问吗?此解决方案非常有效。谢谢您,先生。我想知道你从哪里得到这个答案,或者我从哪里可以学到这些东西。当我阅读firebase文档时,我发现这是一个毫无用处的文档,或者我无法理解它。@user9496018有此信息的文档在这里:我们的一些用户对此新解决方案有问题,lambda从未返回结果。与@PedroPauloAmorim相同:(同样的问题,lambda从未返回结果!