【发布时间】:2015-09-21 15:09:54
【问题描述】:
我的 Android 应用没有在后台接收推送通知,它应该根据documentation。
无需在 Android 设备上运行 Android 应用程序 接收消息。系统会唤醒安卓应用 在消息到达时通过 Intent 广播,只要 应用程序设置有适当的广播接收器和 权限。
尝试不同的通知发现当且仅当通知包含属性“message”时它确实会在关闭时收到推送通知,如果不是,它只是丢弃它。 (推送通知只是 JSON 对象)。
我的通知包含各种属性,包括“alert”、“id”和“title”,但只有“message”才能让 Android 唤醒应用。
不起作用的示例通知:
{ event: 'message',
from: '947957531940',
collapse_key: 'do_not_collapse',
foreground: true,
payload:
{ alert: 'Mensaje de Prueba',
title: 'Titulo Mensaje de Prueba' } }
有效的示例通知:
{ event: 'message',
from: '947957531940',
message: 'Contenido del mensaje de prueba.',
collapse_key: 'do_not_collapse',
foreground: true,
payload:
{ alert: 'Mensaje de Prueba',
title: 'Titulo Mensaje de Prueba',
message: 'Contenido del mensaje de prueba.' } }
这是设计上的 Android 标准还是我在我的应用中做错了什么?
我的应用是使用 Ionic 和 Cordova 开发的。
PD:对不起,我的英语不好。
编辑:
这是app.js中.run模块内的Android推送代码,如ng-cordova指令指定:
if (ionic.Platform.isAndroid()){
var androidConfig = {
"senderID": "94*************",
"ecb": "window.casosPush"
};
try{
var pushNotification = window.plugins.pushNotification;
} catch (ex){
}
// Llamada en caso de exito
var successfn = function(result){
//alert("Success: " + result);
};
// Llamada en caso de error
var errorfn = function(result){
window.alert("Error: " + result);
};
// Llamada de casos de notificacion push
window.casosPush = function(notification){
switch (notification.event){
case 'registered':
if (notification.regid.length > 0){
$rootScope.data.token = notification.regid;
//alert('registration ID = ' + notification.regid);
}
break;
case 'message':
$rootScope.mensajes.unshift(notification.payload);
$localstorage.setArray('mensajes', $rootScope.mensajes);
alert(JSON.stringify(notification));
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
};
try{
// Llamada de registro con la plataforma GCM
pushNotification.register(successfn,errorfn,androidConfig);
} catch(notification){
}
}
【问题讨论】:
-
你的“关闭”是什么意思?设置中的“强制关闭”?如果是,那么以后就不会再推送了。
-
@TeeTracker 否,不是强制关闭,只是未激活,在后台,当前在屏幕中未激活。我很清楚,如果应用程序被强制关闭,推送通知将不起作用。
-
你能告诉我你的代码吗?
-
@cfprabhu 是来自 ng-cordova 示例的标准代码,我会更新问题。
标签: android cordova push-notification ionic-framework phonegap-plugins