wolyshaw 4 سال پیش
والد
کامیت
5b12172f1f
2فایلهای تغییر یافته به همراه30 افزوده شده و 20 حذف شده
  1. 8 19
      src/main.js
  2. 22 1
      src/workers/notification.js

+ 8 - 19
src/main.js

@@ -9,27 +9,16 @@ import NotificationWorker from 'worker-loader!./workers/notification.js'
 
 if (typeof Worker === 'function') {
   const notificationWorker = new NotificationWorker()
-  console.log(notificationWorker)
+  notificationWorker.postMessage({
+    type: 'create',
+    title: '测试标题信息',
+    body: '您有新的代办事项'
+  })
 }
 
-// (async function() {
-//   if (Notification.requestPermission) {
-//    const res = await Notification.requestPermission()
-//    if (self.Notification.permission === 'granted') {
-//       const n = new Notification('您有代办事项', {
-//         body: '新的事项提醒',
-//         data: {
-//           url: '/setSilder/setSilder'
-//         }
-//       })
-//       // console.log(n)
-//       n.onclick = evt => {
-//         n.close()
-//         console.log(evt)
-//       }
-//     }
-//   }
-// })();
+window.addEventListener('message', evt => {
+  console.log(evt)
+})
 
 import * as constant from '@/constant'
 

+ 22 - 1
src/workers/notification.js

@@ -1,3 +1,24 @@
+const createMessage = async data => {
+  if (self.Notification.permission === 'granted') {
+    const {title, body, ...rest} = data
+     const n = new Notification(title, {
+       body,
+       data: rest
+     })
+     n.onclick = evt => {
+       n.close()
+       console.log(evt, self, self.postMessage)
+        self.postMessage({
+          type: 'NotificationClicked',
+          data: evt.data
+        })
+     }
+   }
+}
+
 self.addEventListener('message', evt => {
-  console.log(evt)
+  const {type, ...rest} = evt.data
+  if (type === 'create') {
+    createMessage(rest)
+  }
 })