Vueer
Vueer

Reputation: 1500

How to translate Firebase Push Notifications?

I'm sending FCM Push Notifications with Firebase Cloud Messaging, Cloud Functions and Flutter as framework. I'm searching since 3 hours for a solution to receive the background notifications with translations. I want to display the translation in the device language.

What I found are parameters like titleLocKey and bodyLocKey, but I can't find any way to use this parameters. Where in my project or on server environment I have to include this variables?

I fire the notification like this:

// Push Notification
const payload: admin.messaging.MessagingPayload = {
    notification: {
        title: "New User",
        body: "A new user entered your platform",
        badge: "1",
    }
}

fcm.sendToDevice(userToken, payload);

Do you have any idea?

Upvotes: 6

Views: 5191

Answers (1)

dumazy
dumazy

Reputation: 14435

There are two approaches to this.

Send localized content

You might be able to keep track of the user's locale on the server side (inside Cloud Firestore for example) and just choose the right translation there before you send it. This tends to get more tricky when sending a message to multiple users. You could however have the users subscribe to a language specific topic, for example lang_en, lang_fr, ... and send your localized messages to those topics.

Use localization keys

As you already mentioned, there are specific keys available: titleLocKey and bodyLocKey. If you have the correct localization strings set up in the specific platforms, you can use their keys and the platform will figure out which label to show in the notification. For iOS, this is managed in Localizable.strings files, for Android, in res/values/strings.xml.

Upvotes: 7

Related Questions