嗨,我喜欢从 erlang 服务器应用程序发送 FCM 推送通知。当用户在浏览器中订阅 FCM 通知时。swRegistration.pushManager.subscribe() 需要一个公钥(applicationServerKey)。
所以要获得这个密钥,我使用 openssl 并生成两个 PEM 文件私钥和公钥。
openssl ecparam -name prime256v1 -genkey -noout -out es_private_key.pem
openssl ec -in es_private_key.pem -pubout -out es_public_key.pem
我知道你可以从 firebase 控制台获取私钥和公钥。但是提供的密钥不是 PEM 格式,并且签署 JWT 的 erlang 库使用PEM 文件进行签名和验证。
{ok, PrivtPem} = file:read_file("path/to/es_private_key.pem"),
Jwt = jwerl:sign([{name, <<"bob">>}], es256, PrivtPem).
因此,从生成的私人 PEM 密钥中,我可以对 JWT 进行签名,但我需要 base64 字符串格式的 applicationServerKey 将其添加到标头中,并在浏览器中订阅用户。
我应该将 firebase 控制台中的密钥转换为 PEM 或将 PEMS 转换为 firebase 密钥格式。
我花了两天时间尝试用 Erlang 来做这件事。
我发现如何在 Python 中做到这一点,我需要一些在 erlang 中等效的东西。
raw_pub = vapid.public_key.public_bytes(
serialization.Encoding.X962,
serialization.PublicFormat.UncompressedPoint
)
print("Application Server Key = {}\n\n".format(
b64urlencode(raw_pub)))