Javascript 奇怪的钛虫,注册推送通知

Javascript 奇怪的钛虫,注册推送通知,javascript,ios,notifications,titanium-mobile,titanium-alloy,Javascript,Ios,Notifications,Titanium Mobile,Titanium Alloy,这在以前工作正常,但后来变得不稳定 我有一个基本窗口控制器,用于注册推送通知,导航窗口中的所有其他窗口都源于此: //when base window is set up, set up push notifcations //register push notifications function registerPushNotifications() { Titanium.Network.registerForPushNotifications({

这在以前工作正常,但后来变得不稳定

我有一个基本窗口控制器,用于注册推送通知,导航窗口中的所有其他窗口都源于此:

//when base window is set up, set up push notifcations
//register push notifications

    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);
                alert(e);
                var data = e.data;

                var badgeCount = Ti.UI.iPhone.getAppBadge();
                //Will return the app badges
                badgeCount = badgeCount + 1;
                //Incrementing the appbadge
                Ti.UI.iPhone.setAppBadge(badgeCount);
                //Setting new appbadge

                var message = data.message;

                /*
                if (message != '') {
                var my_alert = Ti.UI.createAlertDialog({
                title : '',
                message : message
                });
                my_alert.show();
                }
                */

                var location = data.path;
                var parts = location.split('/');
                var index = parts.length - 1;

                //get type id
                var id = parts[index];
                var type = parts[0];

                //if push notification came from background, open window if not current window
                if (e.inBackground == 1) {


                        } else {

                            //update view directly after entering app from the background.

        });

    };
此函数用于注册推送通知

直到最近,我的应用程序才开始变得不稳定。每当我收到推送通知时,如果我注销,则重新登录并在此过程中再次启动registerPushNotifications功能。它将继续触发旧的推送通知,e.inBackground属性设置为1。尽管没有在后台点击推送通知

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);
            alert(e);
            var data = e.data;

            var badgeCount = Ti.UI.iPhone.getAppBadge();
            //Will return the app badges
            badgeCount = badgeCount + 1;
            //Incrementing the appbadge
            Ti.UI.iPhone.setAppBadge(badgeCount);
            //Setting new appbadge

            var message = data.message;
        }});

};

registerPushNotifications();

$.winLogin.open();
我尝试在注销时注销推送通知,如下所示:

} else if (e.row.name == '_logout') {
    Ti.API.info('Logout');
    Alloy.Globals.facebookModule.logout();
    //alert(Alloy.Globals.navGroup);
    var login = Alloy.createController('index', {}).getView();
    login.open();
    //clear base controllers
    Alloy.Globals.navGroup.close();
    Alloy.Globals.baseController.close();
    Alloy.Globals.baseController = null;
    Ti.Network.unregisterForPushNotifications();
但是我没有运气

我还尝试通过删除应用程序、更改日期时间并在对话框再次返回时单击确定以使用推送通知来重置推送通知。但它不起作用

因此,我不知道该怎么办——以及为什么会出现这个问题。我已将代码还原回代码的旧版本。同样的问题

知道为什么会这样吗,干杯

更新:

var deviceToken = null;


if(Alloy.Globals.testFlag==0){  
Ti.Network.registerForPushNotifications({
    // Specifies which notifications to receive
    types: [
        Ti.Network.NOTIFICATION_TYPE_BADGE,
        Ti.Network.NOTIFICATION_TYPE_ALERT,
        Ti.Network.NOTIFICATION_TYPE_SOUND
    ],
    success: deviceTokenSuccess,
    error: deviceTokenError,
    callback: receivePush
});
// Process incoming push notifications
function receivePush(e) {
    alert('Received push: ' + e);
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
    deviceToken = e.deviceToken;
    Alloy.Globals.testFlag = 1;
}

function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}

}
这个bug真的很奇怪,令人沮丧

我刚刚进一步隔离了它,如果我在前台收到推送通知,它似乎会出问题

如果我回到负责注册推送通知的窗口,前台推送通知将继续执行,但这次认为它来自后台

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);
            alert(e);
            var data = e.data;

            var badgeCount = Ti.UI.iPhone.getAppBadge();
            //Will return the app badges
            badgeCount = badgeCount + 1;
            //Incrementing the appbadge
            Ti.UI.iPhone.setAppBadge(badgeCount);
            //Setting new appbadge

            var message = data.message;
        }});

};

registerPushNotifications();

$.winLogin.open();
非常令人沮丧的错误-非常卡住(

更新2:

var deviceToken = null;


if(Alloy.Globals.testFlag==0){  
Ti.Network.registerForPushNotifications({
    // Specifies which notifications to receive
    types: [
        Ti.Network.NOTIFICATION_TYPE_BADGE,
        Ti.Network.NOTIFICATION_TYPE_ALERT,
        Ti.Network.NOTIFICATION_TYPE_SOUND
    ],
    success: deviceTokenSuccess,
    error: deviceTokenError,
    callback: receivePush
});
// Process incoming push notifications
function receivePush(e) {
    alert('Received push: ' + e);
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
    deviceToken = e.deviceToken;
    Alloy.Globals.testFlag = 1;
}

function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}

}
我已经包装了一个全局变量,它是推送通知事件侦听器寄存器周围的一个标志

我再也不会有这种错误了

因此,似乎每次打开(和关闭)窗口时,都会复制此事件侦听器,从而导致奇怪的行为


我不知道为什么会发生这种情况,当关闭的窗口应该从内存中删除时,事件侦听器也应该如此。或者事件侦听器的行为是这样的-一旦在应用程序中设置,它就会存储在全局内存中?

这不是你的错,这是Tianium NetworkModule.m中的错误

NetowrkModule.m中的函数“registerForPushNotifications”位于函数末尾

// check to see upon registration if we were started with a push 
// notification and if so, go ahead and trigger our callback
id currentNotification = [[TiApp app] remoteNotification];
if (currentNotification!=nil && pushNotificationCallback!=nil)
{
    NSMutableDictionary * event = [TiUtils dictionaryWithCode:0 message:nil];
    [event setObject:currentNotification forKey:@"data"];
    [event setObject:NUMBOOL(YES) forKey:@"inBackground"];
    [self _fireEventToListener:@"remote" withObject:event listener:pushNotificationCallback thisObject:nil];
}
如果将remoteNotification作为变量缓存在TiApp.m中,它将在pn注册后直接触发事件

您可以注释掉该部分代码,或者在返回推令牌之前强制放弃所有回调事件