【发布时间】:2021-06-29 14:28:49
【问题描述】:
我正在尝试使用 oneSignal 使用令牌 ID 从我的设备发送文本,我的 tokenId 在 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 的数据已正确添加到firebase 的“聊天”集合中。我只是想不通使用一个信号sendNotification
【问题讨论】:
标签: firebase flutter dart onesignal