gpt4 book ai didi

javascript - 尝试使用带有 TTL 的谷歌 FCM 发送数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:00 25 4
gpt4 key购买 nike

我一直在尝试通过 Google 的 FCM 发送数据,该数据将在 10 秒后过期。我的意思是,如果用户在这 10 秒内没有连接到互联网,他将不会收到它。

这是我用于向 FCM 发送请求的 Javascript 代码:

var http = new XMLHttpRequest();
http.open("POST", "https://fcm.googleapis.com/fcm/send", true);
http.onreadystatechange = function() {};
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "key=*****");
http.send(JSON.stringify({
"to": "/topics/all",
"data": {
"Title": "Test",
"areas": "Dan, Ayalon",
},
"android": {
"ttl": "10s" //I've tried also removing the s, and changing the ttl to TTL and time_to_leave
}
}));

问题是我的 android 应用程序在 10 秒过去后甚至 2 分钟后仍然收到数据。 (我在这里所做的是关闭我手机的网络并在 2 分钟后重新打开它)

我尝试通过 Firebase 控制台发送请求,结果成功了……一分钟后我没有收到消息。我在这里做错了什么?

谢谢!

更新:我想到了另一个解决方案。也许我会手动发送当前时间,然后让客户端检查过去了多少时间?这听起来是个不错的解决方案吗?

更新 2:我尝试了 Google 在他们的文档中提供的每个示例,但没有一个有效。我还尝试了一百万种其他方法来编写请求,但没有任何效果。我开始觉得这可能是一个错误。我报告了谷歌,现在我正在等待他们的回应。我将在这里更新他们的回复。

最佳答案

这是我从 Google 的 Firebase 支持部门收到的答复,他们发现我在这里做错了什么:

Looking at your code, it seems that you have some irregularities with your request format. The headers are for the Legacy FCM API while the format of the payload is for the HTTP v1 FCM API.

If you want to use the Legacy API, you'll have to omit the Android specific payload and use time_to_live instead of ttl. But if you prefer using the HTTP v1 API, you'll have to build the request as mentioned here and use the token parameter instead of to.

问题是我结合了两种请求方法。请求的 header 与旧版 API 的格式匹配,而请求本身采用 HTTP v1 FCM API 格式。

这部分请求属于 HTTP v1 API 格式:

"android": {
"ttl": "10s"
}

虽然正确的方法是不使用 "android" 标签,而只使用 time_to_live 标签:

"time_to_live": 10 //The time in seconds

这就是我的完整请求现在的样子:

var http = new XMLHttpRequest();
http.open("POST", "https://fcm.googleapis.com/fcm/send", true);
http.onreadystatechange = function() {};
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "key=*****");
http.send(JSON.stringify({
"to": "/topics/all",
"data": {
"Title": "Test",
"areas": "Dan, Ayalon",
},
"time_to_live": 10
}));

有什么问题欢迎评论

关于javascript - 尝试使用带有 TTL 的谷歌 FCM 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49020074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com