Reputation: 13
So I have this function that I want it to send notifications based on user location using OneSignal REST API, it's my first time dealing with notification and one-signal, so I check https://documentation.onesignal.com/docs I spend the last 7 H trying :(, what I found is I can sort my audience using filters, fortunately, I didn't understand How to use it, sorry if my English not clear enough, thanks in advance my_function
public function sendNotification(){
$content = array(
"en" => 'Test Message'
);
$fields = array(
'app_id' => "My_app_id",
'filters' => array(array("field" => "location", "key" => "radius", "relation" => "=", "value" => "50"),array("operator" => "or"),array("field" => "location", "key" => "lat", "relation" => "=", "value" => "3.1946047"),array("operator" => "or"),array("field" => "location","key"=>"long", "relation" => "=", "value" => "30.2402744")),
'data' => array("foo" => "bar"),
'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 NmZhZDk4MDMtZTJlZ'));
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["allresponses"] = $response;
return $return = json_encode( $return);
}
Upvotes: 1
Views: 601
Reputation: 2951
Your code is not formatted as requested in the doc: https://documentation.onesignal.com/reference#send-to-users-based-on-filters
This corrected 'filters'
will filter all users within a radius of a certain location:
$fields = array(
'app_id' => "My_app_id",
'filters' => [
[
"field" => "location",
"radius" => "50",
"lat" => "3.1946047",
"long" => "30.2402744"
]
],
'data' => array("foo" => "bar"),
'contents' => $content
);
Upvotes: 1