1

Im使用Phonegap,Backbone.js和Require.js構建應用程序。該應用程序實現了Phonegap推送通知。目前,在index.html的腳本的負載是這樣的:帶有Phonegap和推送通知的Require.js用於iOs

<script type="text/javascript" src="cordova.js"></script> 
<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script> 

<script type="text/javascript" src="js/app/index.js"></script> 
<script type="text/javascript"> 
    app.initialize(); 
</script> 

<script data-main="js/app" src="js/require.js"></script> 

index.js看起來是這樣的:

var app = { 
// Application Constructor 
initialize: function() { 
    this.bindEvents(); 
}, 


// Bind Event Listeners 
bindEvents: function() { 

    document.addEventListener('deviceready', this.onDeviceReady, false); 
}, 


onDeviceReady: function() { 

    var pushNotification = window.plugins.pushNotification; 
    pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"}); 

}, 


errorHandler:function(error) { 
    //alert('in errorHandler'); 
    //alert(error); 
}, 

/* 
* 
* For iOS 
*/   
tokenHandler:function(status) { 

    //save the status to server 

}, 


onNotificationAPN: function(event) { 

//display alert 

}, 

}; 

在tokenHandler,我想打電話給我定義的模型作爲Require.js模塊。所以,我將index.js與Require.js集成在一起。 index.html的成爲本:

<script type="text/javascript" src="cordova.js"></script> 
<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script> 

<script data-main="js/app" src="js/require.js"></script> 

的index.js文件現在看起來是這樣的:

define(function (require) { 

var app = { 
    // Application Constructor 
    initialize: function() { 
    this.bindEvents(); 
    }, 


    // Bind Event Listeners 
    bindEvents: function() { 

    document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 


    onDeviceReady: function() { 

    var pushNotification = window.plugins.pushNotification; 
    pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"}); 

    }, 


    errorHandler:function(error) { 
    //alert('in errorHandler'); 
    //alert(error); 
    }, 

    /* 
    * 
    * For iOS 
    */   
    tokenHandler:function(status) { 

     //save the status to server 

    }, 


    onNotificationAPN: function(event) { 

    //display alert 

    }, 

}; 

return app; 
}); 

的在app.js,我做的:

... ... ...

require(['jquery', 'backbone', 'app/router', 'app/index'], function ($, Backbone, Router, Index) { 

var router = new Router(); 
Index.initialize(); 

Backbone.history.start(); 

}); 

的問題發生在回調pushNotification.register()這是app.onNotificationAPN。隨着index.js的要求模塊的加載,這將導致一個錯誤:

processMessage failed: Error 

當我用戶在撥打電話到app.onNotificationAPN的匿名函數,我也得到了同樣的錯誤。

正確的回調是什麼?

回答

1

我有類似的問題,只是我的onNotificationAPN沒有被調用。我使用本指南作爲參考(設置註冊呼叫) - Push Notifications guide

嘗試使用指南的方式添加回調函數。 您也可以將我的推送通知處理程序看作是requirejs模塊。它工作正常:) 順便說一句,我用Durandal與淘汰賽建設我的應用程序。

在我的index.html中,我引用了PushNotification.js,該文件也在我的項目中。

的Index.html:

<body> 
<script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
<script type="text/javascript" src="Scripts/jquery/jquery-2.0.3.min.js"></script> 
<!-- PhoneGap plugins --> 
<script type="text/javascript" charset="utf-8" src="Scripts/phoneGap/PushNotification.js"></script> 
.... 
<script type="text/javascript" src="Scripts/require.js"></script> 
<script> 
    var useragent = navigator.userAgent.toLowerCase(); 
if (useragent.match(/android/) || useragent.match(/iphone/) || useragent.match(/ipad/) || useragent.match('ios')) { 
document.addEventListener('deviceready', onDeviceReady, false); 
    } 
    else { 
     onDeviceReady(); 
    } 

    function onDeviceReady() { 
     .... 

     require.config({ 
      baseUrl: 'App', 
      paths: { 
       "main": "main" 
      } 
     }); 
     require(["main"]); 
    }; 
</script> 

和推送通知元件:

define([ 
'knockout' 
], function (
ko 
) { 
var pushNotification = window.plugins.pushNotification; 

function addCallback(key, callback) { 
    if (window.callbacks === undefined) { 
     window.callbacks = {}; 
    } 
    window.callbacks[key] = callback; 
}; 

function registerDevice() { 
    pushNotification.register(
    tokenHandler, 
    errorHandler, { 
     "badge": "true", 
     "sound": "false", 
     "alert": "true", 
     "ecb": "callbacks.notificationHandler" 
    }); 
}; 

// result contains any message sent from the plugin call 
function successHandler(result) { 
    alert('result = ' + result); 
}; 

// result contains any error description text returned from the plugin call 
function errorHandler(error) { 
    alert('error = ' + error); 
}; 

function tokenHandler(result) { 
    // Your iOS push server needs to know the token before it can push to this device 
    // here is where you might want to send it the token for later use. 
    console.log('post token to rikardo', result); 

    svc.post('Notification', ko.toJSON({ DeviceToken: result })); 
    addCallback('notificationHandler', onNotificationAPN); 
}; 

// iOS 
function onNotificationAPN(event) { 
    var model = {}, 
     type = event.type; 

    if (event.inAppMessage) 
     model = JSON.parse(event.inAppMessage); 

    if (type == 'AchievementViewModel') { 
     pushModalHandler.addItem(model); 
     pushModalHandler.displayModals('achievement'); 
    } 

    if (type == 'TimeQuestViewModel') { 
     pushModalHandler.addItem(model); 
     pushModalHandler.displayModals('timeQuest'); 
    } 
}; 

return { 
    registerDevice: registerDevice 
}; 
}); 

我希望這有助於!