Onesignal include_external_user_ids 在发送推送通知 API

有人请帮助我使用存储在 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

        );

    }

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


对你的帮助表示感谢。


守候你守候我
浏览 159回答 1
1回答

动漫人物

我终于通过使用 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("/^\"(.*)\"$/","$1",$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");
打开App,查看更多内容
随时随地看视频慕课网APP