中的其他变量
看这段代码,我想知道是什么触发了onNotificationGCM
?它在应用程序注册时被触发,但它什么时候被触发,比如说,当我想向用户推送消息时?我有一个聊天应用程序,我想在聊天进入时推送一条消息。所以我明白我注册了设备,但我假设,这段代码需要用新事件再次运行。我只需要了解零件流程和零件代码。
// handle GCM notifications for Android
$window.onNotificationGCM = function (event) {
switch (event.event) {
case 'registered':
if (event.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.
var device_token = event.regid;
RequestsService.register(device_token).then(function(response){
alert('registered!');
});
//send device reg id to server
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (event.foreground) {
console.log('INLINE NOTIFICATION');
var my_media = new Media("/android_asset/www/" + event.soundname);
my_media.play();
} else {
if (event.coldstart) {
console.log('COLDSTART NOTIFICATION');
} else {
console.log('BACKGROUND NOTIFICATION');
}
}
navigator.notification.alert(event.payload.message);
console.log('MESSAGE -> MSG: ' + event.payload.message);
//Only works for GCM
console.log('MESSAGE -> MSGCNT: ' + event.payload.msgcnt);
//Only works on Amazon Fire OS
console.log('MESSAGE -> TIME: ' + event.payload.timeStamp);
break;
case 'error':
console.log('ERROR -> MSG:' + event.msg);
break;
default:
console.log('EVENT -> Unknown, an event was received and we do not know what it is');
break;
}
};
看一下这个例子:
case 'message':
/*
if (e.foreground) {
window.alert("Message recieved");
}
else {
if (e.coldstart) {
window.alert("Coldstart Notification");
} else {
window.alert("Background Notification");
}
}
window.alert("Notification message: " + e.payload.message
+ "nn Time: " + e.payload.conversation);
*/
var data = e.payload;
if (data.conversation){
window.history.replaceState(null, '', '#/chats/');
ProjectName.conversation.load(data.conversation);
}
if (data.product){
window.history.replaceState(null, '', '#/product/' + data.product);
}
break;
case 'error':
// window.alert("Notification error: " + e.msg);
break;
default:
// window.alert("Notification - Unknown event");
break;
}
e.payload
包含您发送到应用程序的所有数据,包括message
。您可以访问case 'message'