Cordova 需要使用Phonegap.js和iOs推送通知

Cordova 需要使用Phonegap.js和iOs推送通知,cordova,push-notification,requirejs,apple-push-notifications,Cordova,Push Notification,Requirejs,Apple Push Notifications,我正在用Phonegap、Backbone.js和Require.js构建一个应用程序。该应用程序实现Phonegap推送通知。目前,index.html中脚本的加载如下所示: <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" charset="utf-8" src="PushNotification.js"></script

我正在用Phonegap、Backbone.js和Require.js构建一个应用程序。该应用程序实现Phonegap推送通知。目前,index.html中脚本的加载如下所示:

<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>
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>
在app.js中,我执行以下操作:

。。。 ...

问题发生在对pushNotification.register()的回调中,该回调是app.onNotificationAPN。将index.js作为Require模块加载时,会导致错误:

processMessage failed: Error
当我使用匿名函数代替对app.onNotificationAPN的调用时,我也会得到相同的错误


正确的回调应该是什么

我也有类似的问题,只是我的onNotificationAPN没有接到电话。我使用本指南作为参考(设置注册呼叫)——

尝试使用向导方式添加回调函数。 您还可以将我的推送通知处理程序作为requirejs模块进行查看。它工作正常:) 顺便说一句,我正在使用Durandal和knockout构建我的应用程序

在我的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>
我希望这有帮助

processMessage failed: Error
<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
};
});