订阅后获取 OneSignal UserID
Posted
技术标签:
【中文标题】订阅后获取 OneSignal UserID【英文标题】:Get OneSignal UserID after subscribe 【发布时间】:2017-09-26 21:41:45 【问题描述】:我正在使用 OneSignal 来获取通知,因此根据文档,我制作了这样的 SDK 代码:
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script>
var OneSignal = window.OneSignal || [];
OneSignal.push(["init",
appId: "myappID",
safari_web_id: "myID",
autoRegister: false,
promptOptions:
/* My prompt options */
,
welcomeNotification:
//my options
,
notifyButton:
enable: true,
showCredit: false,
prenotify: true,
position: 'bottom-left',
text:
/*My text options */
,
colors: // My custom colors
]);
OneSignal.push(function()
OneSignal.showHttpPrompt();
OneSignal.getUserId().then(function(userId)
console.log("OneSignal User ID:", userId);
);
);
</script>
显示幻灯片提示消息效果很好,但订阅后我需要刷新网站才能在控制台中获取用户 ID。如何修改此代码功能以在订阅后立即在控制台中获取用户 ID?
【问题讨论】:
【参考方案1】:您需要为订阅更改事件添加一个监听器。否则 getUserId 调用将在用户接受订阅之前触发。这对我有用:
var OneSignal = window.OneSignal || [];
OneSignal.push(function()
OneSignal.on('subscriptionChange', function (isSubscribed)
console.log("The user's subscription state is now:",isSubscribed);
OneSignal.push(function()
OneSignal.getUserId(function(userId)
console.log("OneSignal User ID:", userId);
);
);
);
);
OneSignal.push(["init",
appId: "myappID",
safari_web_id: "myID",
autoRegister: false,
promptOptions:
/* My prompt options */
,
welcomeNotification:
//my options
,
notifyButton:
enable: true,
showCredit: false,
prenotify: true,
position: 'bottom-left',
text:
/*My text options */
,
colors: // My custom colors
]);
【讨论】:
以上是关于订阅后获取 OneSignal UserID的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android studio 中以编程方式取消订阅 OneSignal