Engineering

12 October, 2017

Android Push Notifications With Firebase: Translations and Troubleshooting

While implementing android push notifications, it’s very likely that your app will need to support multiple languages, so how will you translate them?

António Valente

Software Engineer

Android Push Notifications With Firebase: Translations and Troubleshooting

It has been a while since I’ve started working as an Android Developer here at Coletiv. I really enjoy developing for Android, there is a very good documentation available and a huge community out there with lots of information that helps you almost in any case. But sometimes I find some simple issues that are lacking information. That’s the case with notifications.

To implement notifications in our Android project we’re using Firebase Cloud Messaging (FCM). If you go to the documentation of FCM you will find a pretty good step-by-step guide that helps you set up the Android Client. You’ll configure two services: one to handle the device token registration and another to handle the notification itself if you want to do something with it. This is very straightforward and you’ll have your test device receiving notifications in no time, but you’ll probably have the same 2 problems that I had.

Problem 1: Translations

It’s very likely that your app will need to support multiple languages, so how will you translate the notifications that you send to the user?

It’s very likely that your app will need to support multiple languages, so how will you translate the notifications that you send to the user? You will need to translate this on the user side to know what language the user is using, so it’s not a good idea to simply send some string with the message. Instead, you’ll want to have these messages predefined in the strings.xml resources file and translated into all the supported languages in your project, something like this:

<string name="notification_0">"Cancelled"</string> <string name="notification_1">"Waiting"</string> <string name="notification_2">"Ready"</string> <string name="notification_3">"Complete"</string>

But how will you associate this strings resources with the notifications? That’s easy. You send the notification code in the data payload of the notification and then you’ll retrieve the appropriate string with the correct translation.

Server data payload example:

{ “to”: “adcdef”, “data”: { “action”: “perform_update”, “code”: 2 }, “priority”: 10 }

Your code:

public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { //... String statusCode = remoteMessage.getData().get("code"); String resourceAppStatusString = "notification_".concat(statusCode); int messageId = getResourceId(resourceAppStatusString, "string", getPackageName()); String message = getString(messageId); //... } private int getResourceId(String pVariableName, String pResourceName, String pPackageName) { try { return getResources().getIdentifier(pVariableName, pResourceName, pPackageName); } catch (Exception e) { e.printStackTrace(); return -1; } } } }

This way you’ll be able to show the user the translated message. After this, if you want, you can set a Pending Intent where you’ll define what screen of your app opens when the user clicks the notification.

Then you’ll need to build the Notification object using the Notification Builder where you define its icon, priority, sound and other configurations. After that’s done, you pass it to the Notification Manager using the [notify()](https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)) method and that’s it, your user will receive a properly translated notification.

Problem 2: Testing

While testing the app in debug mode you’ll receive notifications with no problem, but as soon as you stop it to test if you’re receiving notifications while the app is in the background, nothing will happen.

This is really the type of issue I was talking about in the beginning of this post. You are probably running the app in debug mode while developing this feature and hopefully you’ll receive the notifications without a problem, but as soon as you stop the app and close it to test, if the device is receiving the notifications while the app is in the background, nothing will happen, and the reason for that is pretty simple.

The firebase services to handle the tokens and the messages aren’t running because you were running the app in debug mode. After you stop the debugging process in Android Studio, your app will stop completely including all the services. To change this you simply need to start your app normally in the device and then close it, after this you should receive all the notifications you sent because all the services will be running in the background.

If you look at the documentation there is nothing there to help you with these problems… You’ll need to go to the depths of stackoverflow searching over dozens of questions hoping to find some developer that was having the same problem as you.

Hopefully you’ll find this post first and save some of your precious time.

Android

Software Development

Firebase Cloud Messaging

Push Notification

Troubleshooting

Join our newsletter

Be part of our community and stay up to date with the latest blog posts.

Subscribe

Join our newsletter

Be part of our community and stay up to date with the latest blog posts.

Subscribe

You might also like...

Go back to blogNext
How to support a list of uploads as input with Absinthe GraphQL

Engineering

26 July, 2022

How to support a list of uploads as input with Absinthe GraphQL

As you might guess, in our day-to-day, we write GraphQL queries and mutations for Phoenix applications using Absinthe to be able to create, read, update and delete records.

Nuno Marinho

Software Engineer

Flutter Navigator 2.0 Made Easy with Auto Router - Coletiv Blog

Engineering

04 January, 2022

Flutter Navigator 2.0 Made Easy with Auto Router

If you are a Flutter developer you might have heard about or even tried the “new” way of navigating with Navigator 2.0, which might be one of the most controversial APIs I have seen.

António Valente

Software Engineer

Enabling PostgreSQL cron jobs on AWS RDS - Coletiv Blog

Engineering

04 November, 2021

Enabling PostgreSQL cron jobs on AWS RDS

A database cron job is a process for scheduling a procedure or command on your database to automate repetitive tasks. By default, cron jobs are disabled on PostgreSQL instances. Here is how you can enable them on Amazon Web Services (AWS) RDS console.

Nuno Marinho

Software Engineer

Go back to blogNext