Localization of push messages in mobile applications

Having received another mobile application backend project for support, I, unfortunately, came across the fact that the mass mailing of push messages to topics is not localized. On the Internet, I also did not find detailed material on this issue. And everything that I found related to localization on the client side, and this is not always convenient, because It requires pre-determining all possible message options and creating resource files for them with all possible translations.



In my post I will tell you how to localize push messages in topics on the backend side of the mobile application.



In the examples, I will use the firebase-admin library for node.js.



Suppose we need to send a push message to publish news in the application. Naturally, this is best done through the topic. Currently, you can subscribe a device to a topic from the server:



admin.messaging().subscribeToTopic(tokens, 'news'),
      
      







So, you signed up for news. But there's a problem. All clients, regardless of the selected locale, will receive the same push message text. Here a relatively new feature of firebase can come to the rescue - sending messages with filters.



It is very easy to implement. When a client changes the application locale, it is necessary to subscribe the client to the topic with the new locale and (alas) unsubscribe from the topic with the previous locale



  admin.messaging().subscribeToTopic(tokens, req.prams.lang), admin.messaging().unsubscribeFromTopic(user.lang), user.save({ lang: req.prams.lang });
      
      







Now, all that remains to be done is to send messages to topics with the specified filters:



  admin.messaging().send({ ...payloadRu, condition: "'news' in topics && 'ru' in topics" }), admin.messaging().send({ ...payloadEs, condition: "'news' in topics && 'es' in topics" }), admin.messaging().send({ ...payloadEn, condition: "'news' in topics && 'en' in topics" }),
      
      







Such a simple message turned out, in contrast to what I have to do to refactor the project. Because actually the message.



apapacy@gmail.com

October 20, 2019



All Articles