首页 文章

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

提问于
浏览
2

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

我想删除带有响铃图标的通知按钮,并使用带有“您是否要订阅接收更新?”的文本消息的引导警报进行更改 . 只有两个按钮,带有“是”或“否”选项 . 当用户单击“是”然后订阅用户 . 基本上我只想在页面的 Headers 部分中使用订阅按钮,而不是由dafault提供的OneSignal按钮 .

1 回答

  • 6

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

    订阅后如何隐藏通知按钮,或仅在某些页面上显示?通知按钮init选项接受displayPredicate函数,该函数在显示通知按钮之前进行评估 . 要隐藏通知按钮,此函数必须返回值false或解析为false的Promise . 您可以返回任何其他值来显示通知按钮 . 以下是在订阅用户时隐藏通知按钮的示例: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

    使用链接订阅用户以下是一个示例 . 它添加了一个链接,单击该链接会提示用户订阅通知 . 仅当用户取消订阅并且支持通知时,才会显示此链接 . 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>
    

相关问题