Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneSignal - Send scheduled notification

I want to send scheduled messages for OneSignal but instead of using their dashboard I want to use an API to do this and I did read their documentation but I could not find anything is there away to modify their current API so I can send the scheduled notification

function sendMessage(){
        $content = array(
            "en" => $message,

            );

    $fields = array(
        'app_id' => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        'included_segments' => array('All'),
  '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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'));
    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 309
Nouf Avatar asked Aug 14 '17 08:08

Nouf


Video Answer


1 Answers

You can find the scheduling options on this documentation page: https://documentation.onesignal.com/v3.0/reference#section-delivery

Here are the options that are available:

send_after (Send at a later time)

Examples:

"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"

"September 24th 2015, 2:00:00 pm UTC-07:00"

"2015-09-24 14:00:00 GMT-0700"

"Sept 24 2015 14:00:00 GMT-0700"

"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"

delayed_option

Possible values are:

"timezone" (Deliver at a specific time-of-day in each users own timezone)

"last-active" (Deliver at the predicted best time of day to each user).

delivery_time_of_day Use with delayed_option=timezone.

Example:

"9:00AM"

You can add these options to the fields section of your code, like so:

$fields = array(
  'app_id' => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  'included_segments' => array('All'),
  'data' => array("foo" => "bar"),
  'contents' => $content,
  'send_after' => "Sept 24 2017 14:00:00 GMT-0700"
);
like image 158
Gdeglin Avatar answered Oct 02 '22 05:10

Gdeglin