我需要有关基本回调函数的帮助。我有一个引用script.js的主index.html文件。在index.html中,它只是调用app.initialize();从script.js初始化函数。以下是script.js的代码:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
tokenHandler:function(msg) {
console.log("Token Handler " + msg);
alert(msg);
},
errorHandler:function(error) {
console.log("Error Handler " + error);
alert(error);
},
// result contains any message sent from the plugin call
successHandler: function(result) {
alert('Success! Result = '+result)
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var pushNotification = window.plugins.pushNotification;
// TODO: Enter your own GCM Sender ID in the register call for Android
if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"SENDER ID HERE","ecb":"app.onNotificationGCM"});
}
else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
// iOS
onNotificationAPN: function(event) {
var pushNotification = window.plugins.pushNotification;
console.log("Received a notification! " + event.alert);
console.log("event sound " + event.sound);
console.log("event badge " + event.badge);
console.log("event " + event);
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
},
// Android
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
alert('registration id = '+e.regid);
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
PushWoosh.appCode = "CODE GOES HERE";
PushWoosh.register(e.regid, function(data) {
alert("PushWoosh register success: " + JSON.stringify(data));
}, function(errorregistration) {
alert("Couldn't register with PushWoosh" + errorregistration);
});
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
if ( e.foreground )
{
alert(e.payload.message);
alert(e.payload.pagenotification);
}
else
{ // otherwise we were launched because the user touched a notification in the notification tray.
if ( e.coldstart )
{
alert(e.payload.message);
alert(e.payload.pagenotification);
}
else
{
alert(e.payload.message);
alert(e.payload.pagenotification);
}
}
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
};
在onNotificationGCM内部,在前景/冷启动情况下的“消息”情况下,我想将e.payload.pagenotification返回到调用app.initialize()的主index.html页面。有没有人可以解释如何将此信息返回索引页面。
任何帮助将不胜感激,谢谢! - 24x7
答案 0 :(得分:0)
在index.html中有一个全局变量并从foreground / coldstart代码块更新此变量,这样你就可以让变量包含你可以在index.html中使用的信息
答案 1 :(得分:0)
因此事件基本上是在发出事件时将调用的侦听器(回调)列表。
您可以创建自己的应用活动。
有了它,您可以从index page
。
我将尝试根据我对您的问题的理解,使用不同的示例提供帮助。
index.html的来源
<html>
<script type='text/html' src='script.js'></script>
<script type='text/html'>
app.event.addEventListener('notificationGCM', function (e) {
// you can access the event data
console.log(e.detail.x);
});
app.emit('notificationGCM',{ x: 1 });
</script>
</html>
script.js的来源
function myApp() {
this.initialize();
}
myApp.prototype = {
initialize: function () {
this.event = document.createElement('div');
// do your stuff here
},
emit: function (eventName,data) {
var event = new CustomEvent(eventName, { detail: data });
this.event.dispatchEvent(event)
},
onNotificationGCM: function (e) {
// do your stuff here
this.events.notificationGCM.emit('notificationGCM',e);
}
};
var app = new myApp;
在上面的示例中,我创建了一个notificationGCM
事件,但您可以创建所需的任何事件,根据需要发出它们并从index page
收听它们。