我在我的 webview 应用程序中实现了 OneSignal CURL 代码,该代码向每个用户发送通知。
为了根据 OneSignal 的用户 ID 定位特定用户,我尝试使用下面的代码来获取用户 ID
<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
});
});
});
// 输出可能什么也没有订阅变更不是真的。
还有这个:
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);
});
});
//输出'NULL'在用户 ID 上。
我想要做的就是从 OneSignal 获取用户 ID 并在下面的代码中使用它来定位特定用户:
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);
任何帮助是极大的赞赏..