问题
I'm trying to setup OneSignal for notifications on my site. I'd like to use external IDs to keep things simple between my site and OneSignal but I can't seem to get the user to register on OneSignal after accepting notifications.
It sends to the server but it seems to fail with no info.
If I try to read the response from setExternalUserId using then(function (e)) I get nothing, and if I try to directly output it I just get an object:
Setting user playerID as *********
OneSignal's response is [object Promise]
OneSignal thinks playerID is null
Here's the code I'm trying to execute:
<? $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();
}
});
When I check the Users dashboard I don't see any registered users and again my code isn't returning any errors that I can see.
Thank you for your help, OneSignal's documentation is a bit all over the place and very limited in examples.
回答1:
It looks like for whatever reason your PHP isn't properly injecting the correct value at this line: OneSignal.setExternalUserId('<? echo $user_playerid?>');
Please double check that the value is indeed getting put there
来源:https://stackoverflow.com/questions/56813387/user-not-being-registered-when-using-external-id-cant-set-external-id