0

I can not send notifications to a specific user through REST API in PHP. I get The value of Player ID from All users in OneSignal.

PHP code

<?php 

$content = array(
"en" => "messaggio test"
);

$fields = array(
'app_id' => "xxxxxxxx",
'included_segments' =>  array("All"),
'data' => array("foo" => "bar"),
'filters' => array(array('field' => 'tag', 'key' => 
'userId', 'relation' => '=', 'value' => 'xxxxxxxxxxx')),
'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 xxxxxxxxxxxxxxx'));
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);
?>

javascript code

    window.plugins.OneSignal
    .startInit("xxxxxxxxxxx")
    .handleNotificationOpened(notificationOpenedCallback)
    .endInit();

    window.plugins.OneSignal.setSubscription(true);

This code works well by sending notifications to all devices, but not to a single user.

the_martux
  • 885
  • 2
  • 7
  • 18

1 Answers1

1

window.plugins.OneSignal.getIds(function(ids) {
    var device_id = ids.userId;
});
Maat
  • 21
  • 5