我正在尝试使用django-push-notifications设置推送通知,我复制了示例中的大部分代码。我有一个调用enablePushNotifications()
函数的按钮:
export function enablePushNotifications() {
console.log("enabling push notifications...");
if (!'serviceWorker' in navigator){
console.log("Push notifications not supported");
return;
}
navigator.serviceWorker.ready.then(
(registration)=>{
registration.pushManager.subscribe({
userVisibleOnly: true
}).then(
(sub)=>{
console.log(sub);
let endpointParts = sub.endpoint.split("/");
let registration_id = endpointParts[endpointParts.length - 1];
let data = {
'browser': loadVersionBrowser(navigator.userAgent).name.toUpperCase(),
'p256dh': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('p256dh')))),
'auth': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('auth')))),
'registration_id': registration_id,
'endpoint': sub.endpoint,
};
request(
"/api/subscribe",
data
)
}
).catch(
(error)=>console.error(error)
)
}
)
}
当我打开networking选项卡时,我没有看到任何关于传递的属性或头的奇怪信息。具体来说,browser
值是正确的。/api/subscribe
URL的实现如下:
import push_notifications.models as pushmodels
def registerPush(request):
data = requestFormatting.get_data(
request
)
device = pushmodels.WebPushDevice(
name=request.user.username,
active=True,
user=request.user,
registration_id=data["registration_id"],
p256dh=data["p256dh"],
auth=data["auth"],
browser=data["browser"]
)
device.save()
device.send_message("hello")
调用此函数将引发PushError:Push failed:401 Unauthorized。我在chrome和firefox上都进行了测试,都给出了相同的错误。我在设置中指定了我的FCM_API_KEY
,没有其他的。我现在不使用乏味的,虽然我计划在未来。我试过各种不同的代码,但似乎什么都不管用。文档不清楚如何实际初始化设备,我只选择了WebPushDevice
对象,因为它似乎包含与示例中提供的数据相似的属性。你知道吗
这是我第一次尝试使用网页推送,所以一点帮助将不胜感激!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐