Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneSignal - Creating Segments

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

like image 885
Nouf Avatar asked Aug 17 '17 06:08

Nouf


People also ask

How do I send a OneSignal notification to a specific user?

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.

How do you add a test user to OneSignal?

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.

Is OneSignal an API?

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.

What is Oneplayer player ID?

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 .


1 Answers

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");
?>
like image 183
M Junaid Khan Avatar answered Oct 29 '22 16:10

M Junaid Khan