Android Push Notifications With Firebase: Translations and Troubleshooting

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() 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.

Thank you for reading!

Thank you so much for reading, it means a lot to us! Also don’t forget to follow Coletiv on Twitter and LinkedIn as we keep posting more and more interesting articles on multiple technologies.

In case you don’t know, Coletiv is a software development studio from Porto specialised in Elixir, iOS, and Android app development. But we do all kinds of stuff. We take care of UX/UI design, web development, and even security for you.

So, let’s craft something together?