Cant send message with oneSignal, Error: String is not a subtype of type List<String>
Cant send message with oneSignal, Error: String is not a subtype of type List<String>
我正在尝试使用令牌 ID 使用 oneSignal 从我的设备发送文本,我的令牌 ID 在 firebase 控制台中正确显示但是当我尝试发送它时出现错误 type 'String' is not a subtype of type 'List<String>'
请帮我修复它
class _NewMessageState extends State<NewMessage> {
Future<Response> sendNotification(
List<String> tokenIdList, String contents, String heading) async {
return await post(
Uri.parse('https://onesignal.com/api/v1/notifications'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, dynamic>{
"app_id":
"385efff8-************-0fe852ac796c", //kAppId is the App Id that one get from the OneSignal When the application is registered.
"include_player_ids":
tokenIdList, //tokenIdList Is the List of All the Token Id to to Whom notification must be sent.
// android_accent_color reprsent the color of the heading text in the notifiction
"android_accent_color": "FF9976D2",
"small_icon": "ic_stat_onesignal_default",
"large_icon":
"https://www.filepicker.io/api/file/zPloHSmnQsix82nlj9Aj?filename=name.jpg",
"headings": {"en": heading},
"contents": {"en": contents},
}),
);
}
var _enterMessage = '';
//controller for textfield to clear it
final _controller = new TextEditingController();
void _sendMessage() async {
//send message
FocusScope.of(context).unfocus();
final user = FirebaseAuth.instance
.currentUser; //gets the current logged in user gives data like userId
final userData = await FirebaseFirestore.instance
.collection('users')
.doc(user.uid)
.get(); //get username
//create new message
FirebaseFirestore.instance.collection('chat').add(
//no authentication token required
{
'text': _enterMessage,
'createdAt':
Timestamp.now(), //TimeStamp is from cloud firestore package\
'userId': user
.uid, //to seperate chat left and right , differentiate between sender and receiver
'username': userData[
'username'], //the message now fetched will already have usernmae
'userImage': userData['image_url'], //access to url
'tokenId': userData['tokenId'],
}, //createdAT to sort the messages in order
);
_controller.clear(); //clear text field
sendNotification(userData["tokenId"], _enterMessage,
"Testing a message");
}
}
错误出现在 sendNotification(userData["tokenId"], _enterMessage,
具有 tokenId 的数据已正确添加到 'chat' firebase 集合中。我只是想不出使用 onesignal sendNotification
tokenIdList 是字符串列表,id 是数字吗?
对于简单的修复,只需从 List<String>
上删除 List
我正在尝试使用令牌 ID 使用 oneSignal 从我的设备发送文本,我的令牌 ID 在 firebase 控制台中正确显示但是当我尝试发送它时出现错误 type 'String' is not a subtype of type 'List<String>'
请帮我修复它
class _NewMessageState extends State<NewMessage> {
Future<Response> sendNotification(
List<String> tokenIdList, String contents, String heading) async {
return await post(
Uri.parse('https://onesignal.com/api/v1/notifications'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, dynamic>{
"app_id":
"385efff8-************-0fe852ac796c", //kAppId is the App Id that one get from the OneSignal When the application is registered.
"include_player_ids":
tokenIdList, //tokenIdList Is the List of All the Token Id to to Whom notification must be sent.
// android_accent_color reprsent the color of the heading text in the notifiction
"android_accent_color": "FF9976D2",
"small_icon": "ic_stat_onesignal_default",
"large_icon":
"https://www.filepicker.io/api/file/zPloHSmnQsix82nlj9Aj?filename=name.jpg",
"headings": {"en": heading},
"contents": {"en": contents},
}),
);
}
var _enterMessage = '';
//controller for textfield to clear it
final _controller = new TextEditingController();
void _sendMessage() async {
//send message
FocusScope.of(context).unfocus();
final user = FirebaseAuth.instance
.currentUser; //gets the current logged in user gives data like userId
final userData = await FirebaseFirestore.instance
.collection('users')
.doc(user.uid)
.get(); //get username
//create new message
FirebaseFirestore.instance.collection('chat').add(
//no authentication token required
{
'text': _enterMessage,
'createdAt':
Timestamp.now(), //TimeStamp is from cloud firestore package\
'userId': user
.uid, //to seperate chat left and right , differentiate between sender and receiver
'username': userData[
'username'], //the message now fetched will already have usernmae
'userImage': userData['image_url'], //access to url
'tokenId': userData['tokenId'],
}, //createdAT to sort the messages in order
);
_controller.clear(); //clear text field
sendNotification(userData["tokenId"], _enterMessage,
"Testing a message");
}
}
错误出现在 sendNotification(userData["tokenId"], _enterMessage,
具有 tokenId 的数据已正确添加到 'chat' firebase 集合中。我只是想不出使用 onesignal sendNotification
tokenIdList 是字符串列表,id 是数字吗?
对于简单的修复,只需从 List<String>
List