首页 > 解决方案 > 无法使用 oneSignal 发送消息,错误:字符串不是 List 类型的子类型

问题描述

我正在尝试使用 oneSignal 使用令牌 ID 从我的设备发送文本,我的令牌 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,在 Firebase 的“聊天”集合中正确添加了带有 tokenId 的数据。我只是想不通使用一个信号sendNotification

标签: firebaseflutterdartonesignal

解决方案


tokenIdList 是字符串列表,id 为数字吗?


推荐阅读