I have the following code which registers my iOS device with my APNS server:
pushNotification.registerDevice({
alert: true,
badge: true,
sound: true,
pw_appid: "***",
appname: "***"
},
function (status) {
var deviceToken = status['deviceToken'];
},
function (status) {
console.warn('failed to register : ' + JSON.stringify(status));
navigator.notification.alert(JSON.stringify(['failed to register ', status]));
});
This runs onLoad, but I need to access deviceToken outside of the scope of pushNotification.registerDevice() function (status).
Is it possible, in this case, to access deviceToken which is inside a function within a function, outside of the function?
I thought I could make it a global variable, by using window.deviceToken, then calling that later on, but it returns undefined.
If you’re assigning the token to a global and finding its undefined when you try to use it, you will probably find its because the registerDevice ajax hasn’t yet received a response.
Try separating your callback into a separate function, and launch any dependant functions from there.
Personally I would try and pass as an argument, save polluting the global namespace.