// Do NOT call init() twice
// This is just an example
OneSignal.push(["init", {
/* Your other init options here */
notifyButton: {
/* Your other notify button settings here ... */
enable: true,
displayPredicate: function() {
return OneSignal.isPushNotificationsEnabled()
.then(function(isPushEnabled) {
/* The user is subscribed, so we want to return "false" to hide the notify button */
return !isPushEnabled;
});
},
}
}]);
使用链接订阅用户以下是一个示例 . 它添加了一个链接,单击该链接会提示用户订阅通知 . 仅当用户取消订阅并且支持通知时,才会显示此链接 . HTML
<body>
<a href="#" id="subscribe-link" style="display: none;">Subscribe to Notifications</a>
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async="async"></script>
<script>
function subscribe() {
OneSignal.push(["registerForPushNotifications"]);
event.preventDefault();
}
var OneSignal = OneSignal || [];
/* This example assumes you've already initialized OneSignal */
OneSignal.push(function() {
// If we're on an unsupported browser, do nothing
if (!OneSignal.isPushNotificationsSupported()) {
return;
}
OneSignal.isPushNotificationsEnabled(function(isEnabled) {
if (isEnabled) {
// The user is subscribed to notifications
// Don't show anything
} else {
document.getElementById("subscribe-link").addEventListener('click', subscribe);
document.getElementById("subscribe-link").style.display = '';
}
});
});
</script>
</body>
1 回答
以下是OneSignal关于自定义或删除通知按钮的指南:https://documentation.onesignal.com/docs/web-push-prompts#section-subscription-bell
以下是使用您自己的链接/ UI进行订阅提示的指南:https://documentation.onesignal.com/docs/web-push-sdk-setup-https#section-4-customizing-user-subscription