推送插件通知
Push Plugin notification
为什么当我的应用已经 运行 或打开时我收不到推送通知?我正在使用推送插件。
这是我的代码:
<script>
var tokenID = "";
document.addEventListener("deviceready", function(){
//Unregister the previous token because it might have become invalid. Unregister everytime app is started.
// window.plugins.pushNotification.unregister(successHandler, errorHandler);
if(intel.xdk.device.platform == "Android")
{
//register the user and get token
window.plugins.pushNotification.register(
successHandler,
errorHandler,
{
//project ID from Google Cloud Messaging
"senderID":"749289157135",
//callback function that is executed when phone recieves a notification for this app
"ecb":"onNotification"
});
}
}, false);
//app given permission to receive and display push messages in Android.
function successHandler (result) {
// alert('result = ' + result);
}
//app denied permission to receive and display push messages in Android.
function errorHandler (error) {
alert('error = ' + error);
}
//fired when token is generated, message is received or an error occured.
function onNotification(e)
{
switch( e.event )
{
//app is registered to receive notification
case 'registered':
if(e.regid.length > 0)
{
// Your Android push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
// if(localStorage.getItem('deviceID') != "" || localStorage.getItem("deviceID") != NULL || localStorage.getItem("puID") !="" || localStorage.getItem("puID") != NULL){
// window.location = "emergencyCode.html";
// }
// else{
tokenID = e.regid;
localStorage.setItem("deviceID", tokenID);
window.location = "start.html";
}
break;
case 'message':
if ( e.foreground ) {
alert("foreground");
} else {
if ( e.coldstart ) {
// window.location = "login.html";
alert('name = ' + e.payload.name);
alert('lat = ' + e.payload.lat);
alert('lng = ' + e.payload.lng);
} else {
alert("BACKGROUND NOTIFICATION");
}
}
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
</script>
推送通知不起作用的原因可能有很多。确保您的注册 ID、令牌、服务器等有效且有效。请先阅读以下 doc,确保满足您的所有要求。希望这能为您提供一个发现错误的起点。
如果您的应用程序已关闭或打开,PushPlugin 会收到通知。如果您的应用程序已关闭,插件 会在 通知区域 中创建一个通知。但是如果您的应用程序处于打开状态,它会将通知数据传递给您的应用程序而不创建通知。
所以你总是会在配置的 ecb
中收到从你的服务器发送的通知数据,但它不会总是创建通知
为什么当我的应用已经 运行 或打开时我收不到推送通知?我正在使用推送插件。 这是我的代码:
<script>
var tokenID = "";
document.addEventListener("deviceready", function(){
//Unregister the previous token because it might have become invalid. Unregister everytime app is started.
// window.plugins.pushNotification.unregister(successHandler, errorHandler);
if(intel.xdk.device.platform == "Android")
{
//register the user and get token
window.plugins.pushNotification.register(
successHandler,
errorHandler,
{
//project ID from Google Cloud Messaging
"senderID":"749289157135",
//callback function that is executed when phone recieves a notification for this app
"ecb":"onNotification"
});
}
}, false);
//app given permission to receive and display push messages in Android.
function successHandler (result) {
// alert('result = ' + result);
}
//app denied permission to receive and display push messages in Android.
function errorHandler (error) {
alert('error = ' + error);
}
//fired when token is generated, message is received or an error occured.
function onNotification(e)
{
switch( e.event )
{
//app is registered to receive notification
case 'registered':
if(e.regid.length > 0)
{
// Your Android push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
// if(localStorage.getItem('deviceID') != "" || localStorage.getItem("deviceID") != NULL || localStorage.getItem("puID") !="" || localStorage.getItem("puID") != NULL){
// window.location = "emergencyCode.html";
// }
// else{
tokenID = e.regid;
localStorage.setItem("deviceID", tokenID);
window.location = "start.html";
}
break;
case 'message':
if ( e.foreground ) {
alert("foreground");
} else {
if ( e.coldstart ) {
// window.location = "login.html";
alert('name = ' + e.payload.name);
alert('lat = ' + e.payload.lat);
alert('lng = ' + e.payload.lng);
} else {
alert("BACKGROUND NOTIFICATION");
}
}
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
</script>
推送通知不起作用的原因可能有很多。确保您的注册 ID、令牌、服务器等有效且有效。请先阅读以下 doc,确保满足您的所有要求。希望这能为您提供一个发现错误的起点。
PushPlugin 会收到通知。如果您的应用程序已关闭,插件 会在 通知区域 中创建一个通知。但是如果您的应用程序处于打开状态,它会将通知数据传递给您的应用程序而不创建通知。
所以你总是会在配置的 ecb
中收到从你的服务器发送的通知数据,但它不会总是创建通知