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
To send a Push Notification to a user, call our API Create notification method using the include_external_user_ids property if targeting External User Id or include_player_ids property if targeting OneSignal Player Ids.
Using the OneSignal SDK User Data Methods, plugin your device to Xcode or Android Studio and log the player id of your device in the console. Then copy the Player Id without quotes and see above to add it as a Test User.
OneSignal's API is a powerful way to send personalized messages at scale, allowing our customers to send transactional notifications to dynamically-defined user segments.
Player Id. The OneSignal Player Id is a UUID (Unique Universal Identifier) that OneSignal creates per device per OneSignal App Id. The format is lowercase letters and numbers 8 characters-4 characters-4 characters-4 characters-12 characters like b3aaabc2-9a47-4647-adda-3e4583a2d19e .
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");
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With