Firebase Save Notification to DB does not work when the application is not running

I handle FCM notifications in my application. as

 public void onMessageReceived(RemoteMessage remoteMessage) { Notification notification = new Notification(); notification.setTitle(remoteMessage.getNotification().getTitle()); notification.setDescription(remoteMessage.getNotification().getBody()); DateFormat simpledateFormat = new SimpleDateFormat("dd/MM/yyyy"); DateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm"); Date date = new Date(remoteMessage.getSentTime()); notification.setTime(simpleTimeFormat.format(date)); Date date2 = new Date(); notification.setDate(simpledateFormat.format(date2)); DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext()); databaseHelper.saveNotification(notification); //Calling method to generate notification sendNotification(remoteMessage.getNotification().getBody()); } 

but when the application is not working or in the background, I can not save these notifications.

I use Regular Activity to process and display notifications in the application.

as said in this link

I donโ€™t know what I am missing.

+2
source share
1 answer

Android Firebase notifications are not sent to your registered message service when your application is in the background and your notification contains a notification key. To ensure that your service always processes notifications, do not use the notification field. Instead, use the data field for this purpose.

In fact, one drawback of using the Firebase notification field is that you cannot specify large and small icons for notifications in the system tray. It allows you to install only one icon.

Please note that this is not a โ€œuniversalโ€ solution, as iOS users will not receive a notification in the background if there is no notification field.

Having said that, I believe that the best solution right now is to send you Android users a notification of the following type:

 { "to":"tHt4D...", "data": { "title":"Nice title", "body":"Nice body" } } 

and process it in your service as follows:

 public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> data = remoteMessage.getData(); makeMyCustomNotification(data.get("title"), data.get("body")); } 

For iOS users, the standard way:

 { "to":"tHt4D...", "notification": { "title":"Nice title", "body":"Nice body" } } 

This section has a great discussion here.

+2
source

All Articles