gpt4 book ai didi

javascript - OneSignal 自定义提示设置和通知按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:22 26 4
gpt4 key购买 nike

如何自定义默认的 OneSignal 提示设置和通知按钮? enter image description here

我想删除带有铃铛图标的通知按钮,并将其更改为带有文本消息“您要订阅以接收更新吗?”的引导警报。只有一个带有"is"或“否”选项的两个按钮。当用户单击"is"时,然后订阅该用户。基本上我只想在我的页面的标题部分有一个订阅按钮,而不是 OneSignal 默认提供的按钮。

最佳答案

这是 OneSignal 的自定义或删除通知按钮指南:https://documentation.onesignal.com/docs/web-push-prompts#section-subscription-bell

How do I hide the notify button after subscribing, or only show it on certain pages?

The notify button init options takes in a displayPredicate function which is evaluated before the notify button is shown. To hide the notify button, this function must return the value false or a Promise that resolves to the value false. You may return any other value to show the notify button.

Here is an example hiding the notify button if the user is subscribed:

JavaScript

// 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;
});
},
}
}]);

这里是使用您自己的链接/UI 进行订阅提示的指南:https://documentation.onesignal.com/docs/web-push-sdk-setup-https#section-4-customizing-user-subscription

Subscribing Users With a Link

Here is an example. It adds a link that, when clicked, prompts the user to subscribe to notifications. This link is shown only if the user is unsubscribed, and notifications are supported.

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>

关于javascript - OneSignal 自定义提示设置和通知按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com