Onesignal Rest API Push Notification in Wordpress PHP

18-03-2017

We can use following wordpress codes to send notification in OneSignal:

$body = new stdClass();
$body->app_id = "31ee45e2-c63d-4048-903a-89ca43f3afa4";
$body->contents = array('en' => 'merhaba');
$body->filters = array(array('field' => 'tag', 'key' => 'authorID', 'relation' => '=', 'value' => '54'));
$bodyAsJson=json_encode($body);

$response=wp_remote_post("https://onesignal.com/api/v1/notifications", array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array("Content-type" => "application/json;charset=UTF-8",
            "Authorization" => "Basic YzUzNmZkOTAtMmVlMC00OWIzLThlNGQtMzQyYzIyNmFhZjcw"),
        'body' =>$bodyAsJson,
    )
);
echo $response["body"];//{"id":"2565d412-169f-4b7a-83ac-1e2e16d7e61d","recipients":1}

© 2019 All rights reserved. Codesenior.COM