【发布时间】:2016-02-22 20:36:41
【问题描述】:
我正在通过 GCM 向我的应用添加推送通知,但我遇到了一些问题。
当我在前台(打开和屏幕上)使用我的应用发送推送通知时,它会按预期正常显示:
但是当我在后台使用我的应用发送它时,它会显示如下:
所以我的大图像和图标背景颜色不显示。
GCM HTTP 调用代码:
{
"notification": {
"title": "Tecolutla, Veracruz",
"body": "¡Bienvenidos a Tecolutla!",
"icon": "push"
},
"priority": "high",
"data": {
"Mensaje": "Descubre muchísimos lugares en Visita Xalapa.",
"Class" : "Festividades"
},
"to": "/topics/global"
}
GsmListenerService 的代码:
@SuppressWarnings("ConstantConditions")
private void sendNotification(Bundle data) {
Intent intent = new Intent(this, TecolutlaVeracruz.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Push_Title", data.getBundle("notification").getString("title"));
intent.putExtra("Push_Body", data.getBundle("notification").getString("body"));
intent.putExtra("Push_Mensaje", data.getString("Mensaje"));
int Color = ColorManager.getColorBase(Main.INICIO);
if(data.containsKey("Class")){
if(data.getString("Class").equals("Inicio")) Color = ColorManager.getColorBase(Main.INICIO);
else if(data.getString("Class").equals("Nuestro Municipio")) Color = ColorManager.getColorBase(Main.NUESTRO_MUNICIPIO);
else if(data.getString("Class").equals("Festividades")) Color = ColorManager.getColorBase(Main.FESTIVIDADES);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.push)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.acercade_applogo))
.setContentTitle(data.getBundle("notification").getString("title"))
.setContentText(data.getBundle("notification").getString("body"))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setColor(Color)
.setVibrate(new long[]{ 0, 100, 200, 300 })
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我遇到的另一个问题是,如果应用程序被终止或未运行,则不会显示通知。这正常吗?
【问题讨论】:
标签: android push-notification google-cloud-messaging gcmlistenerservice