【问题标题】:Cant send message with oneSignal, Error: String is not a subtype of type List<String>无法使用 oneSignal 发送消息,错误:字符串不是 List<String> 类型的子类型
【发布时间】:2021-06-29 14:28:49
【问题描述】:

我正在尝试使用 oneSignal 使用令牌 ID 从我的设备发送文本,我的 tokenId 在 firebase 控制台中正确显示但是当我尝试发送它时出现错误type 'String' is not a subtype of type 'List&lt;String&gt;' 请帮我修复它

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      对于一个简单的修复,刚刚从 List&lt;String&gt; 中删除了theon List

      【讨论】:

        猜你喜欢
        • 2019-06-26
        • 2022-12-02
        • 2021-03-17
        • 2021-05-10
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 2021-04-07
        相关资源
        最近更新 更多