OneSignal - How to get PlayerId after user subscribes?

15,903

Most likely you're seeing an old user ID. The notificationPermissionChange event occurs too early to obtain the user's ID. The event is for analytics only, and if you want to find the user's ID after subscription, use the subscriptionChange event.

SDK Docs Link: https://documentation.onesignal.com/docs/web-push-sdk

Share:
15,903
user164863
Author by

user164863

Updated on June 04, 2022

Comments

  • user164863
    user164863 about 2 years

    In my javascript code I ask user to subscribe for website notifications using OnceSignal service, so I do:

        OneSignal.registerForPushNotifications();
    

    Then I'm trying to catch PlayerId of subscribed user with the following:

        OneSignal.push(function() {
            OneSignal.on('notificationPermissionChange', function(permissionChange) {
                var currentPermission = permissionChange.to;
    
                if (currentPermission == "granted") {
                    OneSignal.getUserId(function(userId) { 
                        console.log(userId);
                    });
                }
            });
    

    Then I get that userId and store it in my mysql database so I can send notifications later. But for some reason that userId is different from PlayerId stored on OneSignal Server while the format is the same. Are UserId and PlayerId different things? If it is so, how can I get PlayerId after user subscribes for Pushes?