0

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?

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Oct 02 '19 at 22:40

1 Answers1

0

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;
Pawan Verma
  • 1,152
  • 14
  • 22