I am transforming a PWA to a native iOS application. To achieve this I have used pwabuilder.com.
我正在将PWA转换为原生iOS应用程序。为了实现这一点,我使用了pwaBuilder.com。
The current impediment I am facing is the ability to receive FCM push-notifications on my native app. I have successfully implemented Firebase on my native app, but I am stuck on how to link the generated device token to the user of my app.
我目前面临的障碍是无法在我的本机应用程序上接收FCM推送通知。我已经成功地在我的本地应用程序上实现了Firebase,但我在如何将生成的设备令牌链接到我的应用程序的用户上陷入了困境。
In the PWA using Javascript, I could just let Firebase generate the token and send a AJAX-request to my back-end to add the firebase token to the user in the active session.
在使用Java脚本的PWA中,我只需让Firebase生成令牌,并向我的后端发送一个AJAX请求,以便将Firebase令牌添加到活动会话中的用户。
The problem in the native app is that the Firebase Device Token is generated in the Swift-code, but my user authenticates himself in the WebView.
本地应用程序中的问题是Firebase设备令牌是在SWIFT代码中生成的,但我的用户在WebView中对自己进行身份验证。
So I need a secure way to retrieve the authenticated user from Swift's Webview. Or I need a secure way to inject the Firebase Device Token into the webview, so I can use Javascript to send it to the server.
因此,我需要一种安全的方法来从SWIFT的WebView中检索经过身份验证的用户。或者我需要一种安全的方式将Firebase设备令牌注入到Webview中,这样我就可以使用Java脚本将其发送到服务器。
更多回答
优秀答案推荐
Solved. I my Javascript I had the following code:
解决了。在我的Java脚本中,我有以下代码:
function sendFirebaseToken(token) {
let data = {
"token": token,
};
let url = "/update-firebase-token";
$.ajax({
type: 'POST',
url: url,
data: data,
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
}
I can call this code when the token is received in Swift:
当SWIFT收到令牌时,我可以调用此代码:
func sendDeviceTokenToWebView(token: String) {
print("Sending device token to webview");
func checkViewAndEvaluate() {
if (!Hellofood.webView.isHidden && !Hellofood.webView.isLoading ) {
DispatchQueue.main.async(execute: {
Hellofood.webView.evaluateJavaScript("saveFirebaseTokenFromApp('\(token)')")
})
}
else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
checkViewAndEvaluate()
}
}
}
checkViewAndEvaluate()
}
更多回答
我是一名优秀的程序员,十分优秀!