Comments (10)
You can put this in your service worker, in the event.waitUntil:
event.waitUntil(
self.addEventListener('notificationclick', function (event) {
self.clients.openWindow(data.link)
}),
// Show a notification with title 'ServiceWorker Cookbook' and use the payload
// as the body.
self.registration.showNotification(head, {
body: body,
})
);
});
Edit:
Actually, it appears that javascript like it better if the notificationlick event is outiside of another addEventListener, like this:
let url;
// Register event listener for the 'push' event.
self.addEventListener('push', function(event) {
// Retrieve the textual payload from event.data (a PushMessageData object).
// Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation
// on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.
let payload = event.data ? event.data.text() : {"head": "No Content", "body": "No Content", "url": ""},
data = JSON.parse(payload),
head = data.head,
body = data.body,
url = data.url
// Keep the service worker alive until the notification is created.
event.waitUntil(
// Show a notification with title 'ServiceWorker Cookbook' and use the payload
// as the body.
self.registration.showNotification(head, {
body: body,
icon: icon,
})
);
});
self.addEventListener('notificationclick', function (event) {
event.waitUntil(
event.notification.close()
self.clients.openWindow(url),
);
})
from django-webpush.
I'm not sure about what you are asking, but I managed to use icons in my notifications.
In django, when creating the payload, you can do something like this:
payload = {'head': 'Head', 'body': 'Body', 'icon': 'icon url in your server', 'url': 'url that should be open when clicked'}
In the service worker:
let url;
self.addEventListener('push', function(event) {
let payload = event.data ? event.data.text() : {"head": "No Content", "body": "No Content", "icon": "", "url": ""},
data = JSON.parse(payload),
head = data.head,
body = data.body,
icon = data.icon; // You take the icon from the payload.
url = data.url
event.waitUntil(
self.registration.showNotification(head, {
body: body,
icon: icon, //And then you use it the showNotification method.
})
);
});
self.addEventListener('notificationclick', function (event) {
event.waitUntil(
self.clients.openWindow(url),
event.notification.close()
);
})
In my case, I created a model to store notifications, and a ImageField to store the images called "icon". So, when creating the payload in my send_notification() method in my model class, i used icon.url to get the media url:
payload = {'head': self.title, 'body': self.body, 'icon': self.icon.url, 'url': self.url}
from django-webpush.
Tell me, is this not realized yet?
from django-webpush.
@narnikgamarnikus It needs some work in javascript side and I am not that much expert in writing javascript. Can you help on this?
from django-webpush.
@safwanrahman need to use webpush
, and also I need to use "active"
. I'm very busy right now, but when I get to the webpush
, I hope I'll do it.
from django-webpush.
how to add icon and icon path
from django-webpush.
@JLucasRS Thanks a lot for the informations!
I am currently working on jinja template support. Is it possible for you to make a PR?
from django-webpush.
I don't work with jinja, sorry.
from django-webpush.
@JLucasRS ,Thank you very much for your help ,Above code works for me...Thank you again
from django-webpush.
Implemented by @JLucasRS #49. Thanks a lot @JLucasRS
from django-webpush.
Related Issues (20)
- images in notification
- #from django.conf.urls import url HOT 1
- Open webpush url in a popup window
- Migrations problems with Django 3 HOT 5
- New release after 0.3.4 HOT 1
- Grouping of multiple notification not working
- iOS 16.4 Support HOT 4
- Allow passing timeout to pywebpush.send
- Support for IOS 16.4 web push notifications? HOT 4
- IOS 16.4 Support Approach(Solved) HOT 4
- inquiry about notification limits per minute HOT 1
- Subscribe button not working HOT 2
- Errors When Sending Notifications HOT 1
- url not defined HOT 3
- Custom callbacks on subscription
- Cannot read properties of undefined (reding 'pushManager') HOT 4
- Muliple subscriptions for same Device HOT 4
- When I integrate nuxt and django-webpush, it only doesn't work with msedge. HOT 3
- Tag the source HOT 2
- Consider adding defer/async to webpush headers HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-webpush.