arm*_*eys 5 php android ios firebase firebase-cloud-messaging
我想使用Firebase将推送通知发送到本地化的Android和iOS设备。
我意识到我们实际上并没有解决方案,无法向订阅的主题发送本地化消息。假装我有一条消息要发送给订阅“新闻”主题的人,并且有1000人被订阅(他们都说不同的语言),这是我要传达给朝鲜领导人威胁关岛的信息。我希望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"
}
Run Code Online (Sandbox Code Playgroud)
我已经看到了一些可能使用title_loc_key,body_loc_key的引用,但是没有看到关于请求外观的良好示例。这两个参数还暗示着它们可能只是用作应用程序还必须在本地存储和查找的翻译查找键,而且我无法向人们发送全新的现成本地化消息(因为该翻译将预先存储在应用程序中,例如“ april_newsletter_text”)?不知道它是如何工作的,只是想出一些想法。
注意:我正在使用php库来发送Firebase推送通知(Laravel-FCM)。我相信我们团队中的一些开发人员也对该库做了一些扩展,以执行一些自定义任务,因此,如果有一种方法可以直接通过GCM(gcm-http.googleapis.com/gcm/)而不是通过FCM来完成,那么我可以加。
小智 1
如果你使用 firebase,你的 JS 工作线程中有这样的代码
let messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
# Here the callback for web push notification
Run Code Online (Sandbox Code Playgroud)
通常您会在此回调中显示消息。任务是对其进行本地化。因此,您可以只向用户发送有关新消息的消息。获取本地化消息,然后将其显示给用户。在获取时,您将获得 Accept-Language http 标头,或者您可以发送用户 ID。这是我的项目中的示例:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3241 次 |
最近记录: |