我正在尝试为网站上的通知设置OneSignal。 我想使用外部ID使网站和OneSignal之间的事情变得简单,但是在接受通知之后,似乎无法让用户在OneSignal上注册。
它发送到服务器,但似乎没有任何信息而失败。
如果我尝试使用setExternalUserId
来读取then(function (e))
的响应,我什么也没有,如果我尝试直接输出它,我只会得到一个对象:
Setting user playerID as *********
OneSignal's response is [object Promise]
OneSignal thinks playerID is null
这是我要执行的代码:
<? $user_playerid = encryptIDToken($user_id);?>
$(function () {
var OneSignal = window.OneSignal || [];
var u_playerid = null;
OneSignal.push(function () {
OneSignal.init({
appId: "----",
autoRegister: false,
autoResubscribe: true,
persistNotification: true,
welcomeNotification: {
"title": "...",
"message": "..."
}
});
OneSignal.on('notificationPermissionChange', function (permissionChange) {
var currentPermission = permissionChange.to;
console.log("permission equals: " + (currentPermission === 'granted'));
if (currentPermission === 'granted') {
console.log("Setting user playerID as <? echo $user_playerid?>");
OneSignal.setExternalUserId('<? echo $user_playerid?>').then(function (response) {
console.log("OneSignal's response is " + response);
});
OneSignal.getExternalUserId().then(function (id) {
console.log("OneSignal thinks playerID is " + id);
});
}
});
OneSignal.on('subscriptionChange', function (isSubscribed) {
if (isSubscribed == true) {
OneSignal.setExternalUserId('<? echo $user_playerid?>');
OneSignal.getExternalUserId().then(function (id) {
console.log("OneSignal thinks playerID is " + id);
});
}
else if (isSubscribed == false) {
OneSignal.removeExternalUserId();
}
else {
console.log('Unable to process the request');
}
});
});
if (u_playerid != null) {
OneSignal.setSubscription(true);
OneSignal.setExternalUserId('<? echo $user_playerid?>');
}
else {
OneSignal.setSubscription(false);
OneSignal.showNativePrompt();
}
});
当我检查“用户”仪表板时,我看不到任何注册用户,并且我的代码也没有返回我可以看到的任何错误。
感谢您的帮助,OneSignal的文档无处不在,示例非常有限。
答案 0 :(得分:0)
出于任何原因,您的PHP似乎都无法在此行正确注入正确的值:OneSignal.setExternalUserId('<? echo $user_playerid?>');
请仔细检查该值是否确实放在那里