I have a OneSignal CURL code implemented in my webview app which sends notifications to every single user.
To target a specific user based on userid from OneSignal, i tried below code to fetch userID
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
window.OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.init({
appId: "xxxxx",
});
OneSignal.on('subscriptionChange', function (isSubscribed) {
console.log("The user's subscription state is now:", isSubscribed);
OneSignal.getUserId(function(userId) {
console.log("OneSignal User ID:", userId);
// (Output) OneSignal User ID: 270a35cd-4dda-4b3f-b04e-41d7463a2316
});
});
});
// Out put was nothing, maybe subscriptionchange was not true.
And also this:
window.OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.init({
appId: "abbd6373-f394-4912-a314-40493c2a6201",
});
OneSignal.getUserId(function(userid){
console.log(userid);
$(".some").html(userid);
});
});
//Output 'NULL' on userid.
I all want to do is fetch the userid from OneSignal and use it on below code to target specific user:
notify("Alert","Testing Notification");
$content = array("en" => "Testing Notification ");
$fields = array(
'app_id' => "xxxxxx",
'included_segments' => array('All'),
'contents' => $content,
'data' => array("en" => "bar"),
'large_icon' =>"ic_launcher_round.png",
);
$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 xxxxxx'
));
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);
Any help is greatly appreciated..