Reputation: 773
I want to create Segments in oneSignal with out having to access the dashboard so I was wondering if they have an API for that for or any other way to it with me to do it
Upvotes: 6
Views: 2507
Reputation: 904
Yes! This can be done via the OneSignal Create Segments endpoint via the REST API (reference).
Here is an example:
curl -XPOST -H'Authorization: Basic YOUR_REST_API_KEY' -H'Content-Type: application/json' https://onesignal.com/api/v1/apps/YOUR_APP_ID/segments
-d '{"name": "1", "filters": [{"field": "session_count", "relation": ">", "value": "1"},{"operator": "AND"}, {"field": "tag", "relation": "!=", "key": "tag_key", "value": "1"},{"operator": "OR"}, {"field": "last_session", "relation": "<", "value": "30"}]}'
The response would be something like:
// Always true, "id" is uuid of created segment
{"success": true, "id": "7ed2887d-bd24-4a81-8220-4b256a08ab19"}
Upvotes: 0
Reputation: 151
According to their docs[https://documentation.onesignal.com/reference#create-segments] you can call de api but you need a ONESIGNAL PAID PLAN
Upvotes: 0
Reputation: 59
Currently OneSignal does not have any api for creating segment, but if you want to send notifications to specific group you can use tags.
Yes you can send notification to a particular tag, tags can be used as an alternate to segments. If you want to send notification to segment A. set tags for that users to {user:A} and can send notification using this php request.
$fields = array(
'app_id' => YOUR_ONE_SIGNAL_APP_ID,
//'included_segments' => array('plant_a'),
'filters' => array(array("field" => "tag", "key" => "user", "relation" => "=", "value" => "A")),
'data' => array("foo" => "bar"),
'contents' => $content
);
Your complete code will be like
<?PHP
function sendMessage(){
$content = array(
"en" => 'English Message'
);
$fields = array(
'app_id' => YOUR_ONE_SIGNAL_APP_ID,
'filters' => array(array("field" => "tag", "key" => "user", "relation" => "=", "value" => "A")),
'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 REST_API_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);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
Upvotes: 2