Onesignal include_external_user_ids 在发送推送通知 API 请求中格式不正确

Onesignal include_external_user_ids not formatting well in send push notification API request

有人请帮助我使用存储在 onesignal 变量中的多个玩家 ID。

我有一个名为 $onesigId 的变量,它包含以下值字符串:“2”、“3”、“77”、“99”。 在我的 onesignal 发送函数中,我将它设为这样的数组: $onesigId = array($onesigId);但是当我发送通知时,结果如下所示:

JSON sent: {"app_id":"my_app_id","headings":{"en":"Amazing Features"},"include_external_user_ids":["\"2\",\"3\",\"77\",\"99\""],"data":{"foo":"bar"},"url":"https:\/\/testurl.com\/help","contents":{"en":"My testing message"}} JSON received: {"allresponses":"{\"id\":\"\",\"recipients\":0,\"errors\":[\"All included players are not subscribed\"]}"}

下面是我的代码:

 $userids = array();
        $tu = 2;
        $on=1;
        $sql = $db -> prepare('SELECT id FROM users WHERE regTarget=? AND is_active=?');
            $sql -> bind_param('ii', $tu, $on);
            $sql -> execute();
            $result = $sql->get_result();

            while($row = mysqli_fetch_assoc($result) ){
                $userids[]= $row['id'];
                }


                $str = '"' . implode ( '","', $userids ) . '"';

              //echo $str;

                //SEND ONESIGNAL PUSH
                $onesigTitle = "$subject";
                $onesigMessage  = "$message.";

                $onesigId = "$str";

                $onesignalUrl = "$link";

function sendMessage(){
        global $onesigTitle;
        global $onesigMessage;
        global $onesignalUrl;
        global $onesigId;

        $onesigId = array($onesigId);
        $content = array(
            "en" => $onesigMessage
            );

        $fields = array(
            'app_id' => "my_app_id",
            'headings' => array("en" => $onesigTitle),
            'include_external_user_ids' => $onesigId,
            'data' => array("foo" => "bar"),
            'url' => $onesignalUrl,
            'contents' => $content
        );


        $fields = json_encode($fields);

        print("\nJSON sent:\n");
        print($fields);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                                   'Authorization: Basic MY KEY HERE'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);

        return $response;


    }

    $response = sendMessage();
    $return["allresponses"] = $response;
    $return = json_encode( $return);

    print("\n\nJSON received:\n");
    print($return);
    print("\n");

请注意,所有其他变量都按预期工作,只有 $onesigId 出现问题。 当我只添加一个 id 时,它发送成功。我想知道为什么 array("2","3","77", "99") 变成了 "\"2\",\"3\",\"77\",\"99\ “”在响应中以及如何修复它。

非常感谢您的帮助。

我终于通过使用 csv_string_to_array() 将字符串 $str 转换为数组然后将其分配给 onesigId 使其工作。现在一切看起来都很好。

$userids = array();
$tu = 2;
$on=1;
$sql = $db -> prepare('SELECT id FROM users WHERE regTarget=? AND is_active=?');
    $sql -> bind_param('ii', $tu, $on);
    $sql -> execute();
    $result = $sql->get_result();

    while($row = mysqli_fetch_assoc($result) ){
        $userids[]= $row['id'];
        }


        $str = '"' . implode ( '","', $userids ) . '"';

         //This is what I used in converting the string to array
         function csv_string_to_array($str){
          $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
          $results=preg_split($expr,trim($str));
          return preg_replace("/^\"(.*)\"$/","",$results);
          }

      //echo $str;

        //SEND ONESIGNAL PUSH
        $onesigTitle = "$subject";
        $onesigMessage  = "$message.";

        //HERE AM ASSIGNING THE ARRAY TO onesigId
        $onesigId = csv_string_to_array($str);

        $onesignalUrl = "$link";

function sendMessage(){
global $onesigTitle;
global $onesigMessage;
global $onesignalUrl;
global $onesigId;

$content = array(
    "en" => $onesigMessage
    );

$fields = array(
    'app_id' => "my_app_id",
    'headings' => array("en" => $onesigTitle),
    'include_external_user_ids' => $onesigId,
    'data' => array("foo" => "bar"),
    'url' => $onesignalUrl,
    'contents' => $content
);


$fields = json_encode($fields);

print("\nJSON sent:\n");
print($fields);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                           'Authorization: Basic MY KEY HERE'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);
curl_close($ch);

return $response;


}

$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);

print("\n\nJSON received:\n");
print($return);
print("\n");