gpt4 book ai didi

javascript - 如何链接 GCM chrome 推送通知和负载数据?

转载 作者:行者123 更新时间:2023-12-02 14:57:02 26 4
gpt4 key购买 nike

Chrome 中通过 GCM 推送通知让我抓狂。

我已经把一切都准备好了并正在运行。我使用 python 服务器向 GCM 提供推送服务。服务人员可以正常显示推送通知。

据我所知,推送事件没有传递任何数据。听起来好像很快就会推出,但尚未上市。

因此,就在推送通知显示之前,我调用我的服务器以获取推送通知的额外数据。但我没有关于发送到我的服务器以匹配并返回相关数据的推送通知的信息。

我能想到的与通知和用户数据相匹配的所有内容都纯粹是推测。我能找到的最接近的是 PushEvent{} 上的时间戳对象,它大致匹配每个用户 GCM 调用的成功返回。

那么其他人如何处理自定义负载数据以显示 Chrome 推送通知?

PushEvent{} 似乎没有任何与之关联的 ID。我知道推送的用户是谁,因为我之前在注册时存储了该信息。

但是一旦我收到推送,我就不知道推送的目的是什么。

我想避免:

  • 尝试根据时间戳进行匹配(因为通知显示不能保证是即时的)。
  • 尝试为用户提取“最新”数据,因为在我的例子中,可能会同时针对不同的数据位发送多个通知。

Whatsapp 和 Facebook 等其他网站如何将自定义有效负载数据与推送通知导致的看似枯燥的事件数据链接起来?

你做得怎么样?有什么建议么?

这是我的接收器代码:

self.addEventListener('push', function(event) {

event.waitUntil(
fetch("https://oapi.co/o?f=getExtraPushData&uid=" + self.userID + "&t=" + self.userToken).then(function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
throw new Error();
}

return response.json().then(function(data) {
if (data.error || !data.notification) {
console.error('The API returned an error.', data.error);
throw new Error();
}

var title = data.notification.title;
var message = data.notification.message;
var icon = data.notification.icon;

return self.registration.showNotification(title, {
body: message,
icon: icon,
});
});
}).catch(function(err) {
console.error('Unable to retrieve data', err);

var title = 'An error occurred';
var message = 'We were unable to get the information for this push message';
var icon = "https://oapi.co/occurrences_secure/img/stepNotify_1.png";
var notificationTag = 'notification-error';
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag
});
})
);
});

最佳答案

我理解你的问题,当我想使用 chrome 通知时,我一直在摆弄同样的问题。您可以使用indexedDB来保存ObjectStore并在webServices中检索数据。

IndexedDB 可通过 Web 服务访问。我使用它来存储用户信息,当用户收到推送通知时,我会传递存储的访问 key 来识别用户并向他传递相关信息。这是 matt gaunt 的教程,其中表示 Web 服务可以访问索引数据库: http://www.html5rocks.com/en/tutorials/service-worker/introduction/

这是一个很好的 indexedDB 教程: http://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-tutorial/

关于javascript - 如何链接 GCM chrome 推送通知和负载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731007/

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