I'm adding Push Notifications via GCM to my app, but I am stuck on something.
When I send a Push Notification with my app on foreground (open and on screen), it displays normally, as intended:
But when I send it with my app on background, it displays like this:
So my large image and icon background color doesn't display.
Code for GCM HTTP Call:
{
"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"
}
Code for 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());
}
Another issue I have, is that the notification won't display if the app is killed or not running. Is this normal?
So my large image and icon background color doesn't display.
This is because Android now uses Material design and the default icon for push will be completely white. Also, a large icon should always have a background (i.e. avatar). It is displayed on different background colors, therefore it should be a non-transparent picture.
Please refer to this SO questions: Android Notification Large Icon not work and Android Notification Large Icon not work
Another issue I have, is that the notification won't display if the app is killed or not running. Is this normal?
Android notification is implemented as a Service that is started by a BroadcastReceiver. The reason you are no longer receiving notification when the app is force-close is because this Service has also been force-close. The BroadcastReceiver which starts the Service listens to the ACTION_BOOT_COMPLETED event and the ACTION_USER_PRESENT event. This means the server starts at boot and restarts whenever the user unlocks the lock screen.
As stated here:
So if you force-close your app by going to settings, then push notifications will stop working. When you open the app again, then you will receive the notification.