gpt4 book ai didi

ios - 本地化从 Firebase 发送的通知

转载 作者:行者123 更新时间:2023-11-28 07:54:21 27 4
gpt4 key购买 nike

有没有办法翻译/本地化从 Firebase 发送的通知?

我已将我的应用设置为成功接收通知:

extension AppDelegate: UNUserNotificationCenterDelegate {

func setupNotifications() {
registerForRemoteNotifications()
setupNotificationTokenRefresh()
}

func registerForRemoteNotifications() {
let application = UIApplication.shared
UNUserNotificationCenter.current().delegate = self

application.registerForRemoteNotifications()
}

func setupNotificationTokenRefresh() {
storeNotificationToken()

_ = NotificationCenter.default.addObserver(
forName: .MessagingRegistrationTokenRefreshed,
object: nil,
queue: .current
) { [weak self] _ in
self?.storeNotificationToken()
}
}

private func storeNotificationToken() {
Messaging.messaging().token { [weak self] token, error in
if let error = error {
Log.error("Error fetching FCM registration token: \(error)")
} else if let token = token {
// save token
}
}
}

}

负载是从带有数据对象的 Firebase 云函数发送的,我想访问该数据对象并翻译/本地化发送的消息。

我查看了提供的几种方法,但它们似乎只在应用程序位于前台时才拦截通知,这不是我感兴趣的。

从服务器发送的负载:

const payload = {
notification: {
title: 'Friend request',
body: senderName + ' wants to add you as a friend'
},
data: {
senderUserId: friendRequestFrom,
type: 'friendRequest'
}
}

最佳答案

因为您已经在使用云功能,所以实现这一点的一种方法是使用 Google Cloud Translation API 在服务器端进行翻译。 .有一个good sample演示如何使用 Node.js 执行此操作。

例如,当新对象添加到实时数据库中的 /notifications 路径时,您将发送通知。你可以这样做:

const Translate = require('@google-cloud/translate')
const functions = require('firebase-functions')
const projectId = 'YOUR_PROJECT_ID'
const translate = new Translate({
projectId: projectId,
});
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

exports.sendNotification = functions.database.ref(`/notifications/{notif_id}`)
.onWrite(event => {
const notification = event.data.val()
// Don't send when this isn't a new notification.
if (event.data.previous.exists()) {
return null
}
const user_id = notification.user_id
getLocalLanguageOfUser(user_id).then(language => {
if (language != 'en')
translateTo(language, notification).then(localised => {
return send(localised)
}).catch(err => {
return console.log(err)
})
} else { // it's English - no need to translate
return send(notification)
}
})
})

function getLocalLanguageOfUser(user_id) {
return new Promise((resolve, reject) => {
// default will be 'en' for English
firebase.database().ref(`users/${user_id}/language`)
.once('value').then(snapshot => {
resolve(snapshot.val() || 'en')
})
.catch(err => reject(err))
})
}

function translateTo(language, notification) {
return new Promise((resolve, reject) => {
const text = notification.text;
translate.translate(text, language).then(results => {
const translation = results[0];
resolve({
...notification,
text: translation
})
}).catch(err => reject(err))
})
}

function send(notification) {
// use your current setup to send the notification.
// 'text' key will already be localised.
}

关于ios - 本地化从 Firebase 发送的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722718/

27 4 0
文章推荐: jquery - 确定
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com