Titanium Alloy Push Notifications - passing function as callback to be called inside event listener

梦想的初衷 提交于 2019-12-23 01:57:06

问题


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

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