钛合金推送通知 - 传递函数作为在事件侦听器内调用的回调 [英] Titanium Alloy Push Notifications - passing function as callback to be called inside event listener

查看:255
本文介绍了钛合金推送通知 - 传递函数作为在事件侦听器内调用的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个处理推送通知的模块,它具有:

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);
        }; 

场景:我在控制器中使用它,一个与Controller相关的回调(例如,在回调处理之后,我想关闭控制器/视图并打开另一个):

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.
    }

问题是: cback 传递,或者,在调用 CloudPush-callback 事件时, cback 不再存在。
我有以下日志:

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.

推荐答案

试试这一个....

//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);
        }; 

这篇关于钛合金推送通知 - 传递函数作为在事件侦听器内调用的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆