I have implemented Firebase but unfortunately notifications are only working when app is in Foreground or Background but unable to receive any notification when app is closed. I tried surfing online but couldn't get any results. Is there any way to keep app alive in background even after closing it? If yes i think this will help to receive notifications. Any helpful suggestions are entertained. Thanks
Asked
Active
Viewed 614 times
-2
-
Show the codes how you had implemented. – james Jul 11 '17 at 10:43
2 Answers
0
One thing i do is that i don't rely on the notification response, I rather pass the data object and make a custom notification myself.
Here is the code that might help you as we can access the data object even when app is open as well as closed:
Map<String, String> dataMap = remoteMessage.getData();
String notif = dataMap.get("title");
Then i use this function to make a notification
private void notificationManager(Context context, String message) {
try {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Log.v("message", "," + message);
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
builder.setContentTitle(context.getString(R.string.app_name));
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
builder.setContentText(message);
builder.setTicker(message);
builder.setLights(Color.GREEN, 500, 500);
builder.setWhen(when);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(intent);
Notification notification = builder.build();
notification.ledARGB = 0xFFff0000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notificationManager.notify(1, notification);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
} catch (Exception e) {
e.printStackTrace();
}
}
Hope this helps.

Sarthak Gandhi
- 2,130
- 1
- 16
- 23
0
FCM has two messaging services
- Display messaging
- Data messaging
Probably you are using display messaging. You should use data messaging instead of display messaging. Display messaging won't work when app is closed.

Masum
- 4,879
- 2
- 23
- 28