Onesignal include_external_user_ids 在发送推送通知 API 请求时格式不正确
Posted
技术标签:
【中文标题】Onesignal include_external_user_ids 在发送推送通知 API 请求时格式不正确【英文标题】:Onesignal include_external_user_ids not formatting well in send push notification API request 【发布时间】:2020-05-23 21:03:17 【问题描述】:请有人帮助我使用存储在 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\响应中的“”以及如何修复它。
我们将不胜感激。
【问题讨论】:
这是一个关于如何构建数组的 php 问题。此行采用 $onesigId,它是一个包含“2”、“3”的字符串: $onesigId = array($onesigId);这不是您在 php 中创建数组的方式。 谢谢加德纳。我已经包含了构建字符串“2”、“3”、“77”、“99”的部分。我想这可以帮助您帮助我找到问题。我实际上是从我的数据库中获取 id,我想将它们以包含在 onesignal 文档 'include_external_user_ids' => array("xxx","xxx","xxx", "xxx") 中的这种格式放入数组中, 请有人来帮助我。我仍然无法让它工作。 【参考方案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");
【讨论】:
以上是关于Onesignal include_external_user_ids 在发送推送通知 API 请求时格式不正确的主要内容,如果未能解决你的问题,请参考以下文章
php [onesignal] onesignal php api