User not being registered when using external ID/Can't set external ID

半城伤御伤魂 提交于 2021-02-11 12:25:05

问题


I'm trying to setup OneSignal for notifications on my site. I'd like to use external IDs to keep things simple between my site and OneSignal but I can't seem to get the user to register on OneSignal after accepting notifications.

It sends to the server but it seems to fail with no info.

If I try to read the response from setExternalUserId using then(function (e)) I get nothing, and if I try to directly output it I just get an object:

Setting user playerID as *********
OneSignal's response is [object Promise]
OneSignal thinks playerID is null

Here's the code I'm trying to execute:

<? $user_playerid = encryptIDToken($user_id);?>

        $(function () {

            var OneSignal = window.OneSignal || [];
            var u_playerid = null;

            OneSignal.push(function () {

                OneSignal.init({
                    appId: "----",
                    autoRegister: false,
                    autoResubscribe: true,
                    persistNotification: true,
                    welcomeNotification: {
                        "title": "...",
                        "message": "..."
                    }
                });


                OneSignal.on('notificationPermissionChange', function (permissionChange) {
                    var currentPermission = permissionChange.to;
                    console.log("permission equals: " + (currentPermission === 'granted'));
                    if (currentPermission === 'granted') {
                        console.log("Setting user playerID as <? echo $user_playerid?>");
                        OneSignal.setExternalUserId('<? echo $user_playerid?>').then(function (response) {
                            console.log("OneSignal's response is " + response);
                        });

                        OneSignal.getExternalUserId().then(function (id) {
                            console.log("OneSignal thinks playerID is " + id);
                        });
                    }
                });

                OneSignal.on('subscriptionChange', function (isSubscribed) {
                    if (isSubscribed == true) {
                        OneSignal.setExternalUserId('<? echo $user_playerid?>');
                        OneSignal.getExternalUserId().then(function (id) {
                            console.log("OneSignal thinks playerID is " + id);
                        });
                    }
                    else if (isSubscribed == false) {
                        OneSignal.removeExternalUserId();
                    }
                    else {
                        console.log('Unable to process the request');
                    }
                });
            });

            if (u_playerid != null) {
                OneSignal.setSubscription(true);
                OneSignal.setExternalUserId('<? echo $user_playerid?>');
            }
            else {
                OneSignal.setSubscription(false);
                OneSignal.showNativePrompt();
            }
        });

When I check the Users dashboard I don't see any registered users and again my code isn't returning any errors that I can see.

Thank you for your help, OneSignal's documentation is a bit all over the place and very limited in examples.


回答1:


It looks like for whatever reason your PHP isn't properly injecting the correct value at this line: OneSignal.setExternalUserId('<? echo $user_playerid?>');

Please double check that the value is indeed getting put there



来源:https://stackoverflow.com/questions/56813387/user-not-being-registered-when-using-external-id-cant-set-external-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!