Closed saileshkush95 closed 11 months ago
I am setting up only the Web push too and somehow not sure how exactly to set up. Also not sure what API endpoint are there to start using the web push:
For the api endpoints to register the device you need to add the url's from the rest package.
from rest_framework.routers import SimpleRouter
from push_notifications.api.rest_framework import WebPushDeviceViewSet
....
api_router = SimpleRouter()
api_router.register(r'push/web', WebPushDeviceViewSet, basename='web_push')
...
urlpatterns += [
# Api
re_path('api/v1/', include(api_router.urls)),
...
]
On the client/Device you can now send the data for registration with a POST Method to your django.
Something like:
....
}).then(function (sub) {
var endpointParts = sub.endpoint.split('/');
var registration_id = endpointParts[endpointParts.length - 1];
var data = {
'browser': browser.name.toUpperCase(),
'p256dh': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('p256dh')))),
'auth': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('auth')))),
'name': 'XXXXX',
'registration_id': registration_id
};
console.log(data);
requestPOSTToServer(data); <---------
})
....
function requestPOSTToServer(data) {
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://<YOUR_ADDRESS/push/web/");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = () => console.log(xhr.responseText);
xhr.send(data);
}
Then your devices are registered
For the api endpoints to register the device you need to add the url's from the rest package.
from rest_framework.routers import SimpleRouter from push_notifications.api.rest_framework import WebPushDeviceViewSet .... api_router = SimpleRouter() api_router.register(r'push/web', WebPushDeviceViewSet, basename='web_push') ... urlpatterns += [ # Api re_path('api/v1/', include(api_router.urls)), ... ]
On the client/Device you can now send the data for registration with a POST Method to your django.
Something like:
.... }).then(function (sub) { var endpointParts = sub.endpoint.split('/'); var registration_id = endpointParts[endpointParts.length - 1]; var data = { 'browser': browser.name.toUpperCase(), 'p256dh': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('p256dh')))), 'auth': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('auth')))), 'name': 'XXXXX', 'registration_id': registration_id }; console.log(data); requestPOSTToServer(data); <--------- }) .... function requestPOSTToServer(data) { let xhr = new XMLHttpRequest(); xhr.open("POST", "https://<YOUR_ADDRESS/push/web/"); xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onload = () => console.log(xhr.responseText); xhr.send(data); }
Then your devices are registered #658
Thank you. That's basically what I ended up doing. Just wondering why was this so hard for me to do? Will try to share the concepts using the library. Also noticed that there is no Safari browser in the browser list.
@gagamil @viktorisacenko @saileshkush95 Any feedback on this PR, #674, specfically this file, which aims to explain wepush configuration https://github.com/jazzband/django-push-notifications/blob/4fb64be08a5e9d4b9275e9632426d022d5a84a70/docs/WebPush.rst
Fixed by #674
I want to setup push notification for website. I just follwed as described in documenation but Unfurtunately it is now working.
Here is my settings.
and here is my client side code
and here is error snippets.
I don't know what I'm missing??.