【发布时间】:2021-12-25 19:07:48
【问题描述】:
我想使用 PHP 向特定用户发送推送通知。 能够通过 OneSignal 向所有用户发送推送通知,但无法将其发送给特定用户。 已经使用网页视图创建了 android 应用 根据文档,它需要设备 ID,但不知道通过 php 获取。
1> 是否需要为我的应用添加另一个库来推送通知。 2> 如何通过 php 获取推送通知所需的设备 ID 3> 如何为特定用户做推送通知
框架:Laravel 应用:安卓
<?PHP
function sendMessage() {
$content = array(
"en" => 'English Message'
);
$hashes_array = array();
array_push($hashes_array, array(
"id" => "like-button",
"text" => "Like",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
array_push($hashes_array, array(
"id" => "like-button-2",
"text" => "Like2",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
$fields = array(
'app_id' => "my_app_id",
'included_segments' => array(
'Subscribed Users'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'web_buttons' => $hashes_array
);
$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 Auth Key'
));
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);
$data = json_decode($response, true);
print_r($data);
$id = $data['id'];
print_r($id);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
Send push notification to specific user using Parse
Send Notification to specific user using onesignal
Onesignal 文档 https://documentation.onesignal.com/reference/create-notification
【问题讨论】:
标签: android push-notification laravel-7 onesignal