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