推送通知ti.App恢复事件处理程序(钛)

时间:2014-06-20 20:06:31

标签: javascript ios titanium

这是我的代码

function registerPushNotifications() {

    Titanium.Network.registerForPushNotifications({
        types : [Titanium.Network.NOTIFICATION_TYPE_BADGE, Titanium.Network.NOTIFICATION_TYPE_ALERT],
        success : function(e) {
            var deviceToken = e.deviceToken;
            Ti.API.info("Push notification device token is: " + deviceToken);
            Ti.API.info("Push notification types: " + Titanium.Network.remoteNotificationTypes);
            Ti.API.info("Push notification enabled: " + Titanium.Network.remoteNotificationsEnabled);

            Ti.API.error("device Token is: " + e.deviceToken);

            //return device Token to store in Model.
            return e.deviceToken;
        },
        error : function(e) {
            Ti.API.info("Error during registration: " + e.error);
        },
        callback : function(e) {
            // called when a push notification is received.
            //var data = JSON.parse(e.data);
            var data = e.data;

            var badge = data.badge;
            if (badge > 0) {
                Titanium.UI.iPhone.appBadge = badge;
            }
            var message = data.message;
            if (message != '') {
                var my_alert = Ti.UI.createAlertDialog({
                    title : '',
                    message : message
                });
                my_alert.show();
            }

            Ti.App.addEventListener('resume', function() {

alert('do another event if coming from background');


        }
    });

};

根据推送通知是来自后台还是前台我想要运行不同的事件。

我试过了

Ti.App.addEventListener('resume', function() {

但是每次触发我都会从后台触发应用程序(每次发送推送通知时都会堆叠事件处理程序并触发所有这些事件)。而不是仅执行从后台进入的推送通知的回调代码部分。

如何知道推送通知是来自后台还是应用程序正在运行?欢呼声

更新

解决了它。

如果您检查回叫对象,您会发现IsBackground是一个属性,用于检查推送通知的来源。

1 个答案:

答案 0 :(得分:0)

解决方案:

检查isBackground布尔事件的回调的JSON对象。

如果为1表示它是从后台调用的,如果为0,则表示它不是。