Google Cloud Messaging 显示特殊字符错误

问题描述 投票:0回答:2

我有一个 Google Cloud Function (GCF),它通过 Google Cloud Messaging (GCM) 发送推送通知。通知的标题和正文包含特殊字符(ä、ö、ü)。问题是通知无法在智能手机上正确显示这些特殊字符。相反,它显示“??”。我的第一个猜测是 GCF 的 JVM 的字符集是错误的,但我用

Charset.defaultCharset()
System.getProperty("file.enconding")
检查了这一点。两者都返回 UTF-8。但尽管如此,即使日志消息也显示“??”而不是特殊字符。 GCF 环境是否使用默认字符集之外的另一种字符集?

有人知道如何找出答案吗?或者如何解决?

编辑:在我的机器上本地运行该函数没有显示特殊字符的问题。

java character-encoding google-cloud-functions google-cloud-messaging
2个回答
1
投票

解决方案是使用部署命令将构建环境变量设置为标志:

$ gcloud functions deploy my-function ... --set-build-env-vars=JAVA_TOOL_OPTIONS=-Dfile.encoding="UTF-8"

我仍然不明白为什么如果默认字符集也是 UTF-8 且没有该命令标志,那么这是必要的。


0
投票

在 pom.xml 属性中添加源编码可以解决该问题。

 <properties>
   <maven.compiler.target>11</maven.compiler.target>
   <maven.compiler.source>11</maven.compiler.source>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 </properties>
© www.soinside.com 2019 - 2024. All rights reserved.