7

How can I send a notifications if the device is in English and an other notification for all other language ?

I tried to make two different audience but the two audience are empty.

Max0u
  • 691
  • 1
  • 9
  • 21
  • Could you please be more descriptive on what you want to achieve. Thanks. – iamyogish Aug 04 '17 at 15:16
  • @iamyogish I want to send Notification Message according to the language of the device – Max0u Aug 04 '17 at 15:18
  • 1
    If the number of languages is small, you could use [Topic Messaging](https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging). This would require client-side code to subscribe to a topic based on the user locale. A notification would need to be sent multiple times, once for each supported language, to the topic name defined for that language. – Bob Snyder Aug 04 '17 at 15:53

2 Answers2

1

Whenever you send an notification to your user you'll send an static payload which contains an message that needs to be displayed to user when the notification is received.

When notification is received we/iOS can't do any translation of the message from one language to another.

So one solution that I can think of is, You have to find out the language setting or preference (based on his locale) of the user and then you've to store this preference in your backend. Then when you want to send the notification to that user, you can look up his language preference and convert your notification message into that particular language and set the message in payload and send it to the user.

So bottomline is, you've to do this translation of the notification message in your backend and then send it to user using notification.

HTH.

iamyogish
  • 2,372
  • 2
  • 23
  • 40
  • 1
    Yes that what I want to do but how I can manage the target in the Firebase Console ? – Max0u Aug 04 '17 at 15:30
  • @Max0u I don't have much familiarity in Firebase. I am sorry. If you can tell me what is the target in firebase I may be able to help. – iamyogish Aug 04 '17 at 15:38
  • the target is a group of devices. For example I want to target all the devices in English and send the notification in English – Max0u Aug 04 '17 at 15:43
  • Oh alright. How I'd have done this is, whenever I register the device in the Backend (firebase) along with the device token (used to send the notification), I will also send the language preference of the user, you can get the language info from Locale functions. Then you can store the device info along with the language preference. And when you about to send the notification you can look up the language and then translate your message and then send it to user. – iamyogish Aug 04 '17 at 15:57
-3

The best approach for sending push notifications using FCM in multiple language is to encode the message(of any language) in UTF-8 format and send it.

In the android end the message will come like this.

"[30, 45, 13, 54,12, 33]" **these numbers may come in negetive value

Now decode the JSONArray into String is the only work remained.

private static String getStringFromJSONArray(String text)
{
    String decodedText = "";
    Gson converter = new Gson();
    Type type = new TypeToken<List<Byte>>(){}.getType();
    List<Byte> iList = converter.fromJson(text, type);

    byte[] bytes = new byte[iList.size()];

    for(int i = 0; i<iList.size(); i++)
    {
        bytes[i] = iList.get(i);
    }

    decodedText = new String(bytes, StandardCharsets.UTF_8);
    return decodedText;
}

You will get the string in your language.Hope this helps.

AkashToSky
  • 107
  • 1
  • 11
  • This is not what they asked, they asked if they could send different notification content in different languages based on the device language. – Tiebe Groosman Aug 29 '22 at 21:24