我尝试使用其registration_id将消息发送到设备组 这是我的代码:
List<String> tokens=["token1","token2"];
final url='https://fcm.googleapis.com/fcm/send';
http.post(url,headers:{
"Accept": "application/json",
"Authorization":"key=mykey"
,"project_id":"proID"
},
body:
{
"registration_ids" :tokens ,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}
运行应用程序时出现此错误:
Exception has occurred.
_CastError (type 'List<String>' is not a subtype of type 'String' in type cast)
如何解决?
答案 0 :(得分:0)
由于注册需要JSON
字符串,您遇到了问题,但是您正在向其中传递列表对象。您可以通过将List<String>
强制转换为String
来解决此问题。
通过使用toString()
方法转换令牌列表,您将获得一个类似"['token1','token2']"
的字符串
这是修改后的代码:
List<String> tokens=["token1","token2"];
final url='https://fcm.googleapis.com/fcm/send';
http.post(url,headers:{
"Accept": "application/json",
"Authorization":"key=mykey"
,"project_id":"proID"
},
body:
{
"registration_ids" : tokens.toString() ,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}
答案 1 :(得分:0)
问题解决了,我只是编码了身体:
database