0
votes

I am using following PHP script to send OneSignal push notifications to subscribed devices.

<?PHP
function sendMessage(){
    $content = array(
        "en" => 'Testing Message desde el backend small icon '
        );

    $fields = array(
        'app_id' => "xxxx",
        'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
        'data' => array("foo" => "bar"),
        'small_icon' =>"ic_push",


        '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 xxxxx'));
    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");
?>

This script is working fine, and the notifications are sent.

Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.

The segments are "skateboard" and "administrators". How can I add these two segment to the filters array?

1

1 Answers

0
votes

You need to pass segments name array in fields like you are passing tags.

  $content = array(
   "en" => 'Notification Message Here..'
   );

    $heading = array(
        "en" => 'Heading goes here..'
     );

     $fields = array( 
        'app_id' => 'XXXXXXXXXXXXXXXXXXXXX', 
        'contents' => $content,
          'headings' => $heading,
          'included_segments' => array('SegmentName1','SegmentName2'),
          'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
        ); 

    $fields = json_encode($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 XXXXXXXXXXXXXXXXXXX'));

    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;