Update serviceworker notficiations

This commit is contained in:
Ben Halpern 2018-07-03 19:06:23 -04:00
parent 84568cd860
commit eaa13a53fb
4 changed files with 34 additions and 16 deletions

View file

@ -1,4 +1,4 @@
var CACHE_VERSION = 'v2.5.0';
var CACHE_VERSION = 'v2.6.0';
var CACHE_NAME = CACHE_VERSION + ':sw-cache::';
var REQUESTS_LIMIT = 70;
@ -114,21 +114,32 @@ function onFetch(event) {
};
function onPush(event) {
var title = "👋 New Message";
var body; body = event.data.text()
var tag = "push-simple-demo-notification-tag";
var title = 'DEV Connect 👋 '+ Math.random();
var body = event.data.text();
var icon = '<%= image_path "devlogo-pwa-128.png" %>';
var tag = 'simple-push-demo-notification-tag' + Math.random();
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
};
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag,
data: {
url: 'https://dev.to/connect'
}
})
);
};
function onNotificationClick(event) {
if (Notification.prototype.hasOwnProperty('data')) {
var url = event.notification.data.url;
event.waitUntil(clients.openWindow(url));
}
}
self.addEventListener('install', onInstall);
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);
self.addEventListener("push", onPush);
self.addEventListener('push', onPush);
self.addEventListener('notificationclick', onNotificationClick)

View file

@ -54,6 +54,7 @@
border: 1px solid darken($green, 30%);
box-shadow: 3px 3px 0px darken($green, 30%);
background: lighten($green,30%);
margin-bottom: 5px;
&:hover{
background: lighten($green, 20%);
}

View file

@ -96,7 +96,9 @@ export function setupNotifications() {
export function getNotificationState() {
//Not yet ready
return "dont-ask"
if (!window.location.href.includes('ask-for-notifications')){
return "dont-ask"
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {

View file

@ -21,14 +21,13 @@ class Message < ApplicationRecord
end
def send_push
return unless chat_channel.channel_type == "direct"
reciever_ids = chat_channel.chat_channel_memberships.
where.not(user_id: user.id).pluck(:user_id)
PushNotificationSubscription.where(user_id: reciever_ids).each do |sub|
p sub
return if should_send_push?(sub)
Webpush.payload_send(
endpoint: sub.endpoint,
message: message_html,
message: ActionView::Base.full_sanitizer.sanitize(message_html),
p256dh: sub.p256dh_key,
auth: sub.auth_key,
ttl: 24 * 60 * 60,
@ -73,4 +72,9 @@ class Message < ApplicationRecord
errors.add(:base, "You are not a participant of this chat channel.")
end
end
def should_send_push?(sub)
membership = sub.user.chat_channel_memberships.order("last_opened_at DESC").first
membership.last_opened_at > 40.seconds.ago
end
end