4

I want to localize notification , and i find that there is properties for that , like title_loc_key, body_loc_key

he FCM fields used for localizing (I18N) the notification title and body are not working. For example:

{
  "android" : {
    "priority" : "normal",
    "notification" : {
      "title_loc_key": "notify_title",
      "body_loc_key": "notify_body"
    }
  }
}

The FCM documentation states that these fields are used to localize the text in the "app's string resources", but Flutter does not have a res/values/strings.xml file. I am using the flutter_localizations library, and maintain res/values/strings_.arb files as required by Flutter for internationalization, but there is no way for me to tell firebase_messaging to use these resources.

thank you

Fatima ayaa
  • 610
  • 6
  • 16

3 Answers3

4

FCM isn't aware of Flutter. It focuses on the native apps. So for Flutter, this would be the code within the android and ios directories.

If you want to use title_loc_key and body_loc_key, you'll have to add labels to android/app/src/main/res/values/strings.xml on Android. Those are separate from the labels you use within your Flutter code.

dumazy
  • 13,857
  • 12
  • 66
  • 113
3

Localizing push notifications containing bodyLocKey values from Firebase is essentially the same for a Flutter application as it is for a native app. However, the fact that Flutter is a cross platform technology often makes it easy to forget that Flutter projects do still have sections of native code. The statement "fields are used to localize the text in the "app's string resources", but Flutter does not have a res/values/strings.xml file" is not entirely correct. Flutter apps do, in fact, contain a strings.xml file on the Android side. On the iOS side, we can still create a Localizable.strings file the same way we might on native project.

Android Setup

As dumazy pointed out in their answer, the strings.xml file for Android in a Flutter project is located in android/app/src/main/res/values/strings.xml. You can add what amounts to a mapping between your bodyLocKey values and the string values you want to display in the push notifications to this file for Android. For example,

<string name="oven_reached_preheat_temp_notification_title">Oven reached preheat temperature</string>

iOS Setup

Setting up the translation between the bodyLocKey values and string values for iOS is just a bit more complex because we will need to use Xcode to create the Localizable.strings file where the mapping will be stored. Here's the process:

  1. Inside your Flutter project, open the /ios directory in Xcode.
  2. With the Runner selected, go to File > New > File...

Select file > new > File in Xcode

  1. In the dialog box, choose the "Strings file" file type.

Select the "Strings file" file type

  1. Then, name the new file "Localizable.strings"

Name the new file "Localizable.strings"

Here's the trick and the reason we just did that process in Xcode rather than creating the new file in Android Studio (or your IDE of choice). If you take a look at your version control, you will notice that we not only have the new Localizable.strings file, but also a handful of references to this new file in the project.pbxproj file.

Using Xcode to create the file also modifies project.pbxproj

I actually don't fully understand what these references actually do. However, they are key to getting the mapping between your bodyLocKey values and the strings you want to display to actually work.

The final step, of course, is to add your bodyLocKey to string values to the Localizable.strings. For example,

"oven_reached_preheat_temp_notification_title" = "Oven reached preheat temperature";

After rebuilding your app, your FCM message bodyLocKey values should be translated into user-friendly strings.

2

Thanks for both answers, they helped me. Also for time-saving reason want to add, that if you want to add parameters, you should use

iOS, Localizable.strings:

"TRADE_REQUEST" = "You have a new offer %@ from user %@";

And send a message like this:

 "title_loc_key": "TRADE_REQUEST",
 "title_loc_args": [23456, \"John Doe\"],

(I don't know why firebase didn't put it into the docs https://firebase.google.com/docs/cloud-messaging/flutter/receive#ios)

awaik
  • 10,143
  • 2
  • 44
  • 50