简体   繁体   English

如何国际化/本地化您的 FCM 推送通知,尤其是主题?

[英]How to internationalize/localize your FCM push notifications, especially topics?

I'd like to use Firebase to send push notifications to both Android and iOS devices which are localized.我想使用 Firebase 向本地化的 Android 和 iOS 设备发送推送通知。

I realized we didn't really have a solution for sending localized messages to subscribed topics.我意识到我们并没有真正的解决方案来向订阅的主题发送本地化消息。 Pretend I have a message 'North Korea leader threatens Guam' that I want to send to people subscribed to the 'News' topic and 1000 people are subscribed (and they all speak different languages).假设我有一条消息“朝鲜领导人威胁关岛”,我想发送给订阅“新闻”主题的人,并且订阅了 1000 人(他们都说不同的语言)。 I was hoping Firebase records the devices locale when requesting a FCM token, and that I could just send an array of messages per locale to this topic and let FCM/firebase handle, like so:我希望 Firebase 在请求 FCM 令牌时记录设备语言环境,并且我可以将每个语言环境的消息数组发送到该主题并让 FCM/firebase 处理,如下所示:

{
  "default_message": "North Korea leader threatens Guam",
  "en_US": "North Korea leader threatens Guam",
  "en_GB": "North Korea leader threatens Guam",
  "es_MX": "Líder de Corea del Norte amenaza a Guam",
  "fr_FR": "Le chef de la Corée du Nord menace Guam",
  "de_DE": "Nordkorea-Führer droht Guam"
}

I have seen some references to maybe using title_loc_key, body_loc_key but don't see a good example on how the request would look like.我已经看到一些可能使用 title_loc_key、body_loc_key 的参考,但没有看到关于请求看起来如何的好例子。 These 2 params also imply maybe they are just used as a translation lookup key that the app also has to store and lookup locally, and I can't send a brand new off-the-cuff localized message to people (since the translation would have to be stored in the app beforehand like 'april_newsletter_text')?这 2 个参数也意味着它们可能只是用作翻译查找键,应用程序也必须在本地存储和查找,我无法向人们发送全新的现成本地化消息(因为翻译会预先存储在应用程序中,如“april_newsletter_text”)? Not sure how it works, just throwing some thoughts out there.不确定它是如何工作的,只是在那里抛出一些想法。

Note: I am using a php library to do send firebase push notifications (Laravel-FCM).注意:我正在使用 php 库来发送 firebase 推送通知(Laravel-FCM)。 I believe some devs in our team have also extended that library a bit to do some custom tasks so if there is a way to do it directly via GCM (gcm-http.googleapis.com/gcm/) instead of FCM, then I can add.我相信我们团队中的一些开发人员也对该库进行了一些扩展以执行一些自定义任务,因此如果有办法直接通过 GCM (gcm-http.googleapis.com/gcm/) 而不是 FCM 来完成它,那么我可以添加。

If you are using firebase you have code like this in your JS worker如果您使用的是 firebase,则您的 JS 工作器中有这样的代码

let messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
# Here  the callback for web push notification

Typically you would show the message in this callback.通常,您会在此回调中显示消息。 The task is to localise it.任务是对其进行本地化。 So you may just send users the message about a new message.因此,您可以只向用户发送有关新消息的消息。 Fetch localised message and then show it to the user.获取本地化消息,然后将其显示给用户。 On fetch you would get Accept-Language http header or you can send the user id.在获取时,您将获得 Accept-Language http 标头,或者您可以发送用户 ID。 Here is my example from my project:这是我的项目中的示例:

messaging.setBackgroundMessageHandler(function (payload) {

    let data = [];
    try {
        if (payload.data && payload.data.data) {
            data = JSON.parse(payload.data.data);
        }
    } catch (e) {
        Sentry.captureException(e);
    }
    let query = Object.keys(data).map(k => encodeURIComponent(k) + '=' + encodeURIComponent(data[k])).join('&');

    return fetch(self.location.origin + "/web-push/run.json?" + query)
        .then(function (response) {
            # ........................
            #.  Skipped working with https status codes code.
            # ........................
            return response.json();
        })
        .then(function (response) {
            #
            #  Here we have an localised response from the server
            # 
            if (response) {
                return self.registration.showNotification(response.title, {
                    priority: "high",
                    tag: 'renotify',
                    requireInteraction: true,
                    body: response.description,
                    icon: response.icon,
                    image: response.image,
                    url: response.link,
                    click_action: response.link,
                    data: {
                        url: response.link
                    },
                    vibrate: [500, 110, 500, 110, 450, 110, 200, 110, 170, 40, 450, 110, 200, 110, 170, 40, 500]
                })
            }
        })
        .catch(function (err) {
            Sentry.captureException(err);
        });
});

You can combine topics using "conditions" (see docs ).您可以使用“条件”组合主题(请参阅文档)。 These allow you to send your notification to a combination of topics, eg.这些允许您将通知发送到主题组合,例如。

const message = {
    notification: {
        title: 'North Korea leader threatens Guam',
        body: '...'
    },
    condition: `'KoreaNews' in topics && 'LangEnGB' in topics`
};

const messageFr = {
    notification: {
        title: 'Le chef de la Corée du Nord menace Guam',
        body: '...'
    },
    condition: `'KoreaNews' in topics && 'LangFrFR' in topics`
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM