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

时间:2014-07-02 15:56:01

标签: 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({
            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.尽管没有点击后台的推送通知。

我在注销时尝试取消注册推送通知,如下所示:

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

但我没有运气。

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

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

任何想法为什么会这样,欢呼。

更新

这个错误确实非常奇怪和令人沮丧!

我刚刚进一步隔离它,如果我在前台获得推送通知,它似乎就会出错。

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

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

}

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

我不再犯这个错误了。

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

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

1 个答案:

答案 0 :(得分:0)

这不是你的错,这是钛NetworkModule.m中的一个错误。

有一个功能' registerForPushNotifications'在NetowrkModule.m中,在函数末尾,

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

Titanium将remoteNotification缓存为TiApp.m中的变量,它将在pn注册后直接触发事件。

您可以注释掉此部件代码,或在推送令牌返回之前强行放弃所有回调事件。