majorimi / blazor-components

Components collection and extensions for Blazor applications.
MIT License
325 stars 59 forks source link

guide me to Get action in serviceworker plz! #131

Open shaahin18 opened 4 months ago

shaahin18 commented 4 months ago

hi majorimi in your blazor-component notification, i can make notification appearing with 2 actions( copy paste your code!). but when clicking on notification buttons nothing happened. i know i must get that actions(action1,action2) in service worker.but can you explain or reference me to document for understanding how to do things such as go to a page or etc when user clicked on action buttons in notification popup. thanks alot

A9G-Data-Droid commented 2 months ago

The example in the demo code is here: https://github.com/majorimi/blazor-components/blob/master/src/Majorsoft.Blazor.Components.TestApps.Common/wwwroot/sw.js

The actions you specify must be handled in the notificationclick event as described in related documentation:

https://docs.w3cub.com/dom/serviceworkerglobalscope/notificationclick_event

So in the example code you would do various things in JavaScript based on the action clicked:

self.addEventListener('notificationclick', event => {
    let action = event.action;
    let customData = event.notification.data;

    if (action = "action1") {
        !!!DoSomeJavaScriptHere!!!
    }

    let notification = event.notification;
    console.log("Notification clicked: " +  notification);
});

This is why you are passing the URL to a JavaScript file to the HtmlServiceWorkerNotificationOptions. That is where the action handlers live.