@tencentcloud/web-push 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -5
- package/README.md +34 -8
- package/index.d.ts +7 -0
- package/index.esm.js +49 -6
- package/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/core/web-push-sdk.d.ts +1 -0
- package/src/types/outer.d.ts +6 -0
- package/sw.js +1 -1
package/package.json
CHANGED
package/src/types/outer.d.ts
CHANGED
|
@@ -31,6 +31,12 @@ export interface RegisterPushOptions {
|
|
|
31
31
|
* 4 - 无日志级别,SDK 将不打印任何日志
|
|
32
32
|
*/
|
|
33
33
|
logLevel?: LogLevel;
|
|
34
|
+
/**
|
|
35
|
+
* 是否显示系统权限提示弹窗
|
|
36
|
+
* true - 显示系统权限设置提示(默认)
|
|
37
|
+
* false - 不显示系统权限设置提示
|
|
38
|
+
*/
|
|
39
|
+
showSystemPermissionAlert?: boolean;
|
|
34
40
|
}
|
|
35
41
|
export declare enum EVENT {
|
|
36
42
|
MESSAGE_RECEIVED = "message_received",
|
package/sw.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";let t=1;function e(e){return 4!==t&&e>=t}function a(t,...a){e(1)&&console.log(`[WebPush SW] ${t}`,...a)}function i(t,...a){e(2)&&console.warn(`[WebPush SW] ${t}`,...a)}function n(t,...a){e(3)&&console.error(`[WebPush SW] ${t}`,...a)}async function o(t,e={}){const{shouldReport:a=!0,clickEventType:i=2}=e,n={messageID:t.id||Date.now().toString(36)+Math.random().toString(36).substring(2),title:t.title||"New Message",body:t.desc||"You have a new message",icon:t.icon||"",tag:t.tag||t?.id?.slice(-100)||"push-"+Date.now()+"-"+Math.random().toString(36).substring(2,7),data:{
|
|
1
|
+
!function(){"use strict";let t=1;function e(e){return 4!==t&&e>=t}function a(t,...a){e(1)&&console.log(`[WebPush SW] ${t}`,...a)}function i(t,...a){e(2)&&console.warn(`[WebPush SW] ${t}`,...a)}function n(t,...a){e(3)&&console.error(`[WebPush SW] ${t}`,...a)}async function o(t,e={}){const{shouldReport:a=!0,clickEventType:i=2}=e,n={messageID:t.id||Date.now().toString(36)+Math.random().toString(36).substring(2),title:t.title||"New Message",body:t.desc||"You have a new message",icon:t.icon||"",tag:t.tag||t?.id?.slice(-100)||"push-"+Date.now()+"-"+Math.random().toString(36).substring(2,7),data:{...t,clickEventType:i},timestamp:Date.now()},o=self.registration.showNotification(n.title,{body:n.body,icon:n.icon,image:t.image,tag:n.tag,data:n,requireInteraction:!1}),c=s({type:"MESSAGE_RECEIVED",data:n}),l=a&&t.rptURL&&t.rptExt?r({id:t.id,rptURL:t.rptURL,rptExt:t.rptExt,eventType:1}):Promise.resolve();await Promise.all([o,c,l])}async function s(t){try{(await self.clients.matchAll({includeUncontrolled:!0,type:"window"})).forEach(e=>{e.postMessage(t)})}catch(e){n("Failed to send message to clients",e)}}async function r(t){try{if(!t.rptURL||!t.rptExt)return void i("Missing rptURL or rptExt, skipping report");const e={webpushEvents:[{id:t.id,EventType:t.eventType||1,EventTime:Math.floor(Date.now()/1e3),rptExt:t.rptExt}]};a("Reporting WebPush event",{rptURL:t.rptURL,reportData:e});const n=await fetch(t.rptURL,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});n.ok?a("WebPush event reported successfully"):i("Failed to report WebPush event",n.status,n.statusText)}catch(e){n("Error reporting WebPush event",e)}}self.addEventListener("install",t=>{a("Service Worker installing..."),self.skipWaiting()}),self.addEventListener("activate",t=>{a("Service Worker activating..."),t.waitUntil(self.clients.claim())}),self.addEventListener("push",t=>{if(a("Push message received",t),t.data)try{const e=function(t){if(!t)throw new Error("No push data available");try{const e=t.json();return a("Push message data (JSON):",e),e}catch{const e=t.text();return a("Push message data (Text):",e),{id:"",title:"WebPush Notification",desc:e||"You have a new message",url:"/",icon:"",tag:"",image:"",ext:"",rptExt:"",rptURL:""}}}(t.data);t.waitUntil(o(e))}catch(e){n("Failed to process push message",e),t.waitUntil(self.registration.showNotification("WebPush Notification",{body:"You have a new message",tag:"fallback"}))}else i("Push message has no data")}),self.addEventListener("notificationclick",t=>{a("Notification clicked",t);const e=t.notification,o=t.action,c=e.data;if(e.close(),"close"===o)return;const l=s({type:"NOTIFICATION_CLICKED",data:{notification:c,action:o}}),d=async function(t="/"){try{a("Attempting to open or focus window with URL:",t);const n=await self.clients.matchAll({type:"window",includeUncontrolled:!0});let o;a("Found clients:",n.length);try{o=t.startsWith("http://")||t.startsWith("https://")?t:new URL(t,self.location.origin).href}catch(e){i("Invalid URL, using origin:",t,e),o=self.location.origin}a("Full target URL:",o);for(const t of n)if(t.url===o&&"focus"in t)return void(await t.focus());self.clients.openWindow&&await self.clients.openWindow(o)}catch(e){n("Failed to open or focus window",e)}}(c?.data?.url||"/"),f=c?.data?.clickEventType||2,u=c?.data?.rptURL&&c?.data?.rptExt?r({id:c.messageID,rptURL:c.data.rptURL,rptExt:c.data.rptExt,eventType:f}):Promise.resolve();t.waitUntil(Promise.all([l,d,u]))}),self.addEventListener("notificationclose",t=>{a("Notification closed",t)}),self.addEventListener("message",e=>{a("Message received from main thread",e.data);const{type:i,payload:c}=e.data;switch(i){case"SHOW_NOTIFICATION":!async function(t){try{const{eventType:e,data:i,options:n}=t;a("Handling show notification request",{eventType:e,data:i,options:n}),await self.registration.showNotification(n.title,{body:n.body,icon:n.icon,badge:n.badge,tag:n.tag,requireInteraction:n.requireInteraction,silent:n.silent,data:n.data}),a("Notification shown successfully",n.title)}catch(e){n("Failed to handle show notification",e)}}(c);break;case"PROCESS_WEBPUSH_DATA":!async function(t){try{a("Handling webpush data from main thread",t),await o(t,{shouldReport:!1,clickEventType:3}),a("WebPush notification processed successfully")}catch(e){n("Failed to handle webpush data",e);try{await self.registration.showNotification("WebPush Notification",{body:"You have a new message",tag:"fallback"})}catch(i){n("Failed to show fallback notification",i)}}}(c);break;case"REVOKE_MESSAGE":!async function(t){try{(await self.registration.getNotifications()).forEach(e=>{e.data&&e.data.messageID===t&&e.close()}),await s({type:"MESSAGE_REVOKED",data:{messageID:t}})}catch(e){n("Failed to handle message revocation",e)}}(c.messageID);break;case"SET_LOG_LEVEL":l=c.logLevel,t=l,a("Log level updated to:",l);break;case"REPORT_WEBPUSH_EVENT":r(c)}var l}),self.addEventListener("error",t=>{n("Service Worker error",t.error)}),self.addEventListener("unhandledrejection",t=>{n("Service Worker unhandled promise rejection",t.reason)})}();
|