php - Onesignal include_external_user_ids not formatting well in send push notification API request
Get the solution ↓↓↓Someone please help me with using multiple player ids stored in a variable in onesignal.
I have a variable called $onesigId that's holding the following string of values: "2","3","77", "99". In my onesignal send function I made it an array like this: $onesigId = array($onesigId); but when I send the notification the result shows as below:
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\"]}"}
Here's my code below:
$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");
Note that all the other variables are working as expected, just the $onesigId that's misbehaving. When I add just a single id, it sends successfully. I want to know why the array("2","3","77", "99") is turning into "\"2\",\"3\",\"77\",\"99\"" in the response and how to fix it.
Your help will be greatly appreciated.
Answer
Solution:
I finally got it to work by using csv_string_to_array() to convert the string $str to an array and then assign it to the onesigId. Everything looks fine now.
$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");
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.