如何使用One Signal + PHP + Server API发送推送通知?
问题描述:
我正在使用一个信号发送Android应用程序的推送通知。我的问题是如何使用One Signal + PHP + Server API发送推送通知?
如何使用服务器休息API设置发送推送通知?
答
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message'
);
$fields = array(
'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'large_icon' =>"ic_launcher_round.png",
'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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
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");
?>
答
我看到你设置了isAndroid = true,但OneSignal返回一个错误,显示ID为eec33e8e-5774-4b74-9aae-37370778c4b2
的应用程序没有启用Android通知。
请确保您的应用ID是正确的,如果是,则在OneSignal设置中启用Android通知。
在他们的网站上有一个PHP示例:https://documentation.onesignal.com/v2.0/docs/notifications-create-notification。那是你在找什么? – Schore
offcourse,根据我做的文件,但它显示这样的错误 – user3544256
JSON发送:{“app_id”:“eec33e8e-5774-4b74-9aae-37370778c4b2”,“included_segments”:[“All”],“send_after”:“ 2014年5月02日00:00:00 GMT-0700(PDT)“,”data“:{”foo“:”bar“},”isAndroid“:true,”contents“:{”en“:” }}收到的JSON:{“allresponses”:“{\”id \“:\”\“,\”recipients \“:0,\”errors \“:[\”All included players not subscribe \“] \“warnings \”:[\“如果您希望向Android播放器发送消息,您必须在OneSignal设置中配置Android通知。”}}“} – user3544256