• Resolved em.kuznetsov

    (@frostyf1re)


    Hello!

    I’m tryin to setup delay to prompt permission message after X visits only.

    But i want to show slidedown permission message with my text and nice buttons yes/now (showHttpPrompt), and if user clicks “Yes” – to show him deafult HTTPS promt which calls by registerForPushNotifications();

    But i can’t correctly modify script from this documentation.

    If i’m changing
    window.OneSignal.registerForPushNotifications();
    to
    window.OneSignal.showHttpPrompt();
    (like in this ticket) – i see this slidedown window, but if i click “yes” – nothing happens.

    I’ve tried to take code from plugin source code and changed:
    window.OneSignal.registerForPushNotifications();
    to:

    window.OneSignal.showHttpPrompt();
    window.OneSignal.init(window._oneSignalInitOptions);

    It works, if i click yes in slidedown permission than i see classic permission request near to the SSL symbol. But in console i see massage about

    SdkInitError {reason: "MultipleInitialization", message: "OneSignal: The OneSignal web SDK can only be initi…remove calls initializing the SDK more than once.", name: "SdkInitError", stack: "SdkInitError: OneSignal: The OneSignal web SDK can…/cdn.onesignal.com/sdks/OneSignalSDK.js:1:147251)"}
    reason: "MultipleInitialization"
    message: "OneSignal: The OneSignal web SDK can only be initialized once. Extra initializations are ignored. Please remove calls initializing the SDK more than once."

    How to correctly modify this script?

    <!-- data-cfasync: Ignore CloudFlare's Rocket Loader, which may impact the triggering of the DOMContentLoaded event (see: https://goo.gl/CvZewv) -->
    <script data-cfasync="false">
      window.OneSignal = window.OneSignal || [];
      var numVisitsTrigger = 3; /* Number of page visits before prompting user */
      
      /* Why use .push? See: https://stackoverflow.com/a/38466780/555547 */
      window.OneSignal.push(function() {
        var numVisits = new Number(localStorage['numVisitsTrigger'] || 0);
        numVisits += 1;
        localStorage['numVisitsTrigger'] = numVisits;
        if (numVisits >= numVisitsTrigger) {
          promptAndSubscribeUser();
        }
      });
      
      function promptAndSubscribeUser() {
        /* Want to trigger different permission messages? See: https://documentation.onesignal.com/docs/permission-requests#section-onesignal-permission-messages */
        window.OneSignal.isPushNotificationsEnabled(function(isEnabled) {
          if (!isEnabled) {        
            window.OneSignal.registerForPushNotifications();
          }
        });
      }
    </script>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi there,

    Sorry for the delay in getting back to you.

    Changing registerForPushNotifications() to showHttpPrompt() should work. Could you please contact us at [email protected] if you’re still facing this issue so we can reproduce the issue on your site?

    I tested showHttpPrompt() just now and I didn’t run into any issues.

    Try clearing your browser data (chrome://settings/clearBrowserData) and try again.

    @frostyf1re

    Have you managed to make this work since then?

    Thread Starter em.kuznetsov

    (@frostyf1re)

    Yes, i only changed registerForPushNotifications() to showHttpPrompt() and it worked.

    I think it was a problem in cache or other browser data which i can’t delete. I installed firefox and it worked correctly. I think it will also work in chrome – i’ll see it soon if subscribers growth will continue. (you can test here https://bmwguide.ru/ – i’ve moved it from test zone to main site).

    I also implemented this script using Google Tag Manager to reduce third-party code in template.

    First i have created a file named onesignaldelay.js with:

    window.OneSignal = window.OneSignal || [];
    var notificationPromptDelay = 12000;
    window.OneSignal.push(function() {
    var navigationStart = window.performance.timing.navigationStart;
    var timeNow = Date.now();
    setTimeout(promptAndSubscribeUser, Math.max(notificationPromptDelay – (timeNow – navigationStart), 0)); });
    function promptAndSubscribeUser() {
    window.OneSignal.isPushNotificationsEnabled(function(isEnabled) {
    if (!isEnabled) {
    window.OneSignal.registerForPushNotifications();
    } }); }

    Then in a second time I added this code in the functions.php file to call the script :

    function theme_js(){
    wp_enqueue_script( ‘onesignaldelay’,
    ‘/wp-content/themes/wp-knowledge-base-child/js/onesignaldelay.js’,
    array() );
    }
    add_action( ‘wp_footer’, ‘theme_js’ );
    I do not get errors in the crawler, but the 12-second timeout in the javascript file seems to have no effect.

    I also changed this :
    window.OneSignal.registerForPushNotifications();

    by :
    window.OneSignal.showHttpPrompt();

    The registration window appears right away, but when I scan the code I get an error message that appears after the 12-second delay

    It’s weird

    I spent 2 hours to find a way to fix it. Solution for delay to show slidedown permission message – disable in plugin configuration “Show the slidedown permission message before prompting users to subscribe”.

    Why it’s not in documentation? And why it’s so hard? This option should be in plugin settings!

    Example – stiabags.ru – 15 seconds

    • This reply was modified 7 years, 3 months ago by Antonio4040.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Setup delay to show slidedown permission message’ is closed to new replies.