OneSignal-Website-SDK icon indicating copy to clipboard operation
OneSignal-Website-SDK copied to clipboard

[Question]: OneSignal.getUserId() doesn't work anymore ?

Open Trungduc79 opened this issue 7 months ago • 4 comments

How can we help?

I have a script which save UserId into Cookie like this. Its worked perfectly. But now OneSignal.getUserId doesn't work anymore. I trie some way and got nothing.

Please help me !

Thank a lot !!

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
  <script>
    function setCookie(cname, cvalue, exdays) {
        const d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        let expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }
    window.OneSignal = window.OneSignal || [];
    OneSignal.push(function() {
        OneSignal.init({
            appId: "<?= ONESIGNAL_KEY ?>",
            allowLocalhostAsSecureOrigin: true
        });
    });
    OneSignal.push(function() {
        OneSignal.getUserId().
        then(response => setCookie('onesignal_player_id', response, 365));
    });
  </script>

Trungduc79 avatar Nov 19 '23 16:11 Trungduc79