问题
I have set up a module for dealing with Push-Notifications which has this:
//myPush module
var CloudPush = require('ti.cloudpush');
...
var setAppPushNotifications = function(cback) {
// Process incoming push notifications
log('cback=' + typeof cback); //log is a wrapper to Ti.API.info
CloudPush.addEventListener('callback', function (evt,cback) {
log('Inside CloudPush-Callback.');
log('cback=' + typeof cback);
getIncomingNotification(evt, cback);
});
};
var getIncomingNotification = function(evt,cback) {
//return if zero payload
//test for app required basic fields
log('cback=' + typeof cback);
cback(evt.payload);
};
Scenario: I use it inside a Controller, so that I can pass a callback that is Controller related (for example, after callback processing I want to close the controller/views and open another one):
//controller code
var myPush = Alloy.Globals.myPush;
...
myPush.setAppPushNotifications(processNotificationPayload);
function processNotificationPayload(p) {
//select notification channel
//do some processing...
//close controller and view and get back to index.
}
The problem is: cback function is not been passed along, or, by the time of CloudPush-callback event is called, cback doesn't exists anymore. I got the following logs:
[myPush.js][setAppPushNotifications]: cback=function
[myPush.js][setAppPushNotifications]: cback=undefined
[myPush.js][getIncomingNotification]: cback=undefined
What would be a solution respecting the given scenario ? Please show me some code. Thanks.
回答1:
try this one....
//myPush module
var CloudPush = require('ti.cloudpush');
...
var setAppPushNotifications = function(cback) {
// Process incoming push notifications
log('cback=' + typeof cback); //log is a wrapper to Ti.API.info
CloudPush.addEventListener('callback', function (evt) {
log('Inside CloudPush-Callback.');
log('cback=' + typeof cback);
getIncomingNotification(evt,cback);
});
};
var getIncomingNotification = function(evt,cback) {
//return if zero payload
//test for app required basic fields
log('cback=' + typeof cback);
cback(evt.payload);
};
来源:https://stackoverflow.com/questions/22759254/titanium-alloy-push-notifications-passing-function-as-callback-to-be-called-in