针对iOs的带有Phonegap和推送通知的Require.js

时间:2014-01-22 17:10:20

标签: 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>

<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加载为Require模块时,会导致错误:

processMessage failed: Error

当我使用匿名函数代替对app.onNotificationAPN的调用时,我也得到了同样的错误。

正确的回调应该是什么?

1 个答案:

答案 0 :(得分: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
};
});

我希望这有帮助!