2017-11-25 122 views
0

我使用Wordpress的Onesignal,其中会员可以通过单击链接订阅通知。如何更改此链接以显示已注册用户的特定消息?OneSignal - 向已订阅的会员显示特定消息

这是我试过的。此代码显示尚未订阅的人的订阅链接。并且不显示已经订阅的人。我的“回声”功能似乎不起作用

有人可以帮忙吗?

<body> 
<a href="#" id="subscribe-link" style="display: none;"></i><b> Subscribe to 
notifications </b></a> 
<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) { echo "You have already subscribed to notifications"; 

      } else { 
       document.getElementById("subscribe-link").addEventListener('click', subscribe); 
       document.getElementById("subscribe-link").style.display = ''; 
      } 
     }); 
    }); 
</script> 

`

回答

0

如果您使用的是WordPress的插件(没有禁用它),那么你并不需要调用registerForPushNotifications。

该插件应为您设置OneSignal变量,以便您可以简单地在要检查的页面上使用isPushNotificationsEnabled方法。

尝试:

OneSignal.push(function() { 
    OneSignal.isPushNotificationsEnabled().then(function(isEnabled) { 
    if (isEnabled) 
     console.log("Push notifications are enabled!"); 
    else 
     console.log("Push notifications are not enabled yet.");  
    }); 
});