与PhoneGap的Require.js和推送通知对于iOS [英] Require.js with Phonegap and Push Notification for iOs
问题描述
林建设有PhoneGap的,Backbone.js的和Require.js的应用程序。该应用程序实现了PhoneGap的推送通知。目前,在index.html的脚本的加载是这样的:
Im building an app with Phonegap, Backbone.js and Require.js. The app implements Phonegap Push Notification. At the moment, the loading of the scripts in index.html looks like this:
<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看起来是这样的:
index.js looks like this:
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的成为本届
In tokenHandler, I want to call a model I have defined as a Require.js module. So, I integrated index.js with Require.js. Index.html became this:
<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文件现在看起来是这样的:
The index.js file now looks like this:
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,我做的:
The in app.js, I do:
...
...
...
... ... ...
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作为需要模块的加载,这导致了一个错误:
The problem occurs in the callback to pushNotification.register(), which is app.onNotificationAPN. With the loading of the index.js as a Require module, this leads to an error:
processMessage failed: Error
当我用户的地方调用app.onNotificationAPN的一个匿名函数,我也得到了同样的错误。
When i user an anonymous function in place of the call to app.onNotificationAPN, I also get the same error.
应该采取什么正确的回调呢?
What should the correct callback be?
推荐答案
我有类似的问题,只是我的onNotificationAPN没有被调用。我用这个指南作为参考(以设置寄存器呼叫) - 的推送通知指南
I had similar problems, just that my onNotificationAPN didn't get called. I used this guide as a reference (to setting up the register-call) - Push Notifications guide
尝试使用指南的方式来添加回调函数。
您还可以看看我的推送通知处理程序作为一个requirejs模块。它工作正常:)
顺便说一句,我使用迪朗达尔与淘汰赛构建我的应用程序。
Try using the guides way to add the callback function. You can also have a look at my push notification handler as a requirejs module. It works fine :) Btw, I'm using Durandal with knockout for building my app.
在我的index.html我必须PushNotification.js的引用,该文件也是在我的项目。
In my index.html I have a reference to PushNotification.js and that file is also in my project.
的index.html:
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
};
});
我希望这有助于!
I hope this helps!
这篇关于与PhoneGap的Require.js和推送通知对于iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!