firebase - Converting Firebase Token to String using Flutter
问题描述
I have an application that is supposed to send the Firebase Token from my Flutter app to an ASP.Net App Server. The endpoint on the app server works - the request from the Flutter app to the App Server is not working.
The reason it is not working is because when I try to send the token, the token doesn't appear to have arrived yet - it's of type Future. How do I turn that token into a string when it finally arrives?
I've tried turning the token directly into a string in the fcmStream.Listen function, I've also tried turning it into a string using _firebaseMessaging.getToken. Neither of them work
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
@override
void initState() {
// TODO: implement initState
super.initState();
location.onLocationChanged().listen((value) {
if (this.mounted) {
setState(() {
currentLocation = value;
});
}
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
String clientToken = _firebaseMessaging.getToken().then((token) {
print("Token Init: " + token.toString());
}
).toString();
BackendService.postToken(clientToken.toString(), "email@gmail.com");
@override
Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
fcmStream.listen((token) {
/*print("Token On Refresh: " + token);
BackendService.postToken(token.toString(), "email@gmail.com");*/
}
);
fcmStream.toString();
class BackendService {
static Future<String> postToken(String token, String hostEmail) async {
final responseBody = (await http.get(
'realurlomitted/.../Meets/RegisterDevice?token=$token&hostEmail=$hostEmail')).body;
print(" Response: " + responseBody.toString());
return responseBody.toString();
}
}
Whenever the token.toString prints, it prints the token just fine - I can see that. It just seems like whenever it tries to make the post using http, the token hasn't arrived from whatever getToken is.
If I can turn that Futrure into a string by awaiting it or something, it would solve my problem so that the $token parameter is the token as a string.
More specifically, my request URL should look like:
https://-----/Meets/RegisterDevice?token=c6V49umapn0:Jdsf90832094890s324&hostEmail=email@gmail.com
But it looks like:
https://-----/Meets/RegisterDevice?token=instance of Future<Dynamic>&hostEmail=email@gmail.com
In the Flutter debugger
解决方案
正如你所说,等待未来会解决你的问题。您可以编写一个async
函数并将代码initState
放入其中并使用await
,或者您可以这样做:
_firebaseMessaging.getToken().then((token) {
final tokenStr = token.toString();
// do whatever you want with the token here
}
);
推荐阅读
- ios - 无法触发长按手势 [UiView]
- regex - 正则表达式在 Alteryx 中不起作用
- c# - 当数组绑定到 UWP 应用程序中的可见性属性时,如何侦听数组中的更改
- reactjs - 如何让 React 类组件在登录后保持其状态,并根据组件中当前可用的状态更改可用路由?
- django - Django Rest 框架和 React Linkedin 社交身份验证 OAuth2Error
- javascript - 有人知道我如何在 NuxtJs 中使用 CkEditor 吗?
- python - Pandas - 用 df.compare 比较两个长度不等的 DF
- html - 有没有办法让我的 Google 字体代码适用于我的 HTML 代码?
- mysql - react-express 应用程序中的 POST 请求的 URL 中显示了表单详细信息
- ios - CoreData 列表项,编辑视图