javascript - OneSignal subscribe user through web page using web-push-sdk -


is there way in 1 signal web-push-sdk add user manually , unsubscribe?

i tried in subscribeonesignal() function nothing happening.

onesignal.push(function() {     onesignal.registerforpushnotifications(); }); 

i have simple html page have 2 buttons 1 "subscribe" , other "unsubscribe", when user click on subscribe button should add @ 1 signal , when clicked on "unsubscribe" button shouldn't receive notifications.

<!doctype html> <html> <head>     <link rel="manifest" href="/manifest.json">     <script src="https://cdn.onesignal.com/sdks/onesignalsdk.js" async></script>     <script>         var onesignal = window.onesignal || [];         onesignal.push(["init", {             appid: "345345-asdf-345",             autoregister: false,             notifybutton: {                 enable: true             }         }]);          function subscribeonesignal(){              onesignal.push(function() {               onesignal.registerforpushnotifications();             });             onesignal.push(function() {               onesignal.registerforpushnotifications({                 modalprompt: true               });             });         }         function unsubscribeonesignal(){              //unsubscribe functionality goes here         }     </script> </head> <body> <p>onesingle testing</p> <br> <button onclick="subscribeonesignal()">subscribe </button> <button onclick="unsubscribeonesignal()">unsubscribe </button>  </body> </html> 

here solution, may else.

<script src="https://cdn.onesignal.com/sdks/onesignalsdk.js" async></script> <script>     var useragentid = null;     var onesignal = window.onesignal || [];     onesignal.push(["init", {         appid: "345345-asdf-345",         autoregister: false,         notifybutton: {             enable: false          },         persistnotification: false     }]);     //firstly check user id      onesignal.push(function() {         onesignal.getuserid().then(function(userid) {                             if(userid == null){                 document.getelementbyid('unsubscribe').style.display = 'none';             }             else{                 useragentid = userid;                 document.getelementbyid('unsubscribe').style.display = '';                 onesignal.push(["getnotificationpermission", function(permission){                 }]);                 onesignal.ispushnotificationsenabled(function(isenabled) {                     if (isenabled){                         document.getelementbyid('unsubscribe').style.display = '';                         document.getelementbyid('subscribe').style.display = 'none';                     }                     else{                       document.getelementbyid('unsubscribe').style.display = 'none';                       document.getelementbyid('subscribe').style.display = '';                     }                 });             }         });     });     //secondly check when subscription changed     onesignal.push(function() {         onesignal.on('subscriptionchange', function (issubscribed) {             if(issubscribed==true){                 onesignal.getuserid().then(function(userid) {                     useragentid = userid;                 }).then(function(){                  // custom function                 // here can send post request php file well.                     onesignalusersubscription(useragentid);                 });                 document.getelementbyid('unsubscribe').style.display = '';                 document.getelementbyid('subscribe').style.display = 'none';             }             else if(issubscribed==false){                 onesignal.getuserid().then(function(userid) {                     useragentid = userid;                 });                 document.getelementbyid('unsubscribe').style.display = 'none';                 document.getelementbyid('subscribe').style.display = '';             }             else{                 console.log('unable process request');             }         });     });     function subscribeonesignal(){         if(useragentid !=null){             onesignal.setsubscription(true);          }         else{             onesignal.registerforpushnotifications({                 modalprompt: true             });         }     }     function unsubscribeonesignal(){         onesignal.setsubscription(false);     } </script> <div id="home-top" class="clearfix"> <p>onesingle testing</p> <br> <button id="subscribe" class="button" onclick="subscribeonesignal()">subscribe </button> <button id="unsubscribe" class="button" onclick="unsubscribeonesignal()">unsubscribe </button> </div> <style> .button {     background-color: #008cba;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;cursor: pointer; } </style> 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -