简体   繁体   English

Angular - Service Worker:如何从路由获取动态 ID 到“Notificationclick”事件中?

[英]Angular - Service worker : How to get dynamic ID from route into the "Notificationclick" event?

I have put some push notifications in my code, in order to someone to be notified when an action is made.我在我的代码中放置了一些推送通知,以便在执行操作时通知某人。 I made the back end with lib.net.webpush 3.1.0 with .net 4.5.2 in C#.我在 C# 中使用带有 .net 4.5.2 的 lib.net.webpush 3.1.0 制作了后端。 So far, the notification system is working very well, but there is something I can't succeed: Within my service worker file (sw.js), using Angular 9, i want, when someone received the notification, that when a user click on it, he is redirect to the page from which it was sent.到目前为止,通知系统运行良好,但有些事情我无法成功:在我的服务工作者文件 (sw.js) 中,使用 Angular 9,我希望,当有人收到通知时,当用户点击在它上面,他被重定向到发送它的页面。

first i made it like it (as i read in the doc):首先我让它喜欢它(正如我在文档中读到的):

self.addEventListener('notificationclick', function (event) {
const urlToOpen = new URL(event.notification.data, self.location.origin).href;

event.notification.close();

event.waitUntil(clients.openWindow(urlToOpen));
 });

But i was always redirect to the "localhost://4200" and not to the complete url.但我总是重定向到“localhost://4200”而不是完整的 url。

In order to test, i made this:为了测试,我做了这个:

self.addEventListener('notificationclick', function (event) {
const urlToOpen = new URL(self.location.origin + "/#/rdc/rapports/2015");

event.notification.close();

event.waitUntil(clients.openWindow(urlToOpen));
});

and it worked.它奏效了。 But i can't get the dynamic URL.但我无法获得动态 URL。 Does anyone knows how to get a dynamic URL within this file?有谁知道如何在此文件中获取动态 URL ? Or how to be redirect to the page that sent the notification?或者如何重定向到发送通知的页面? I also tried something like this here: notificationclick event service worker But i really dont understand.我也在这里尝试过这样的事情: notificationclick event service worker但我真的不明白。 Thanks.谢谢。

I just succeed in doing it.我只是成功地做到了。 So, for anyone in the same case as I was: within the service worker, you can't access the DOM, i wasn't able to get any ID or any path I was trying to aim in my code.因此,对于与我相同情况的任何人:在服务工作者中,您无法访问 DOM,我无法获得任何 ID 或我试图在我的代码中瞄准的任何路径。 The solution was to, in my C# code, to add a "URL" property and parameter to my "SendNotification" funtion.解决方案是,在我的 C# 代码中,将“URL”属性和参数添加到我的“SendNotification”函数中。 Then, when i got a user, i can target his ID because it's already here.然后,当我有一个用户时,我可以定位他的 ID,因为它已经在这里了。 Then, in my Angular code (within the service worker file), I just had to to this (i am storing my url in "data" here):然后,在我的 Angular 代码中(在服务工作者文件中),我只需要这样做(我将 url 存储在此处的“数据”中):

self.addEventListener('notificationclick', function (event) {   console.log("notif click event", event);

    const urlToOpen = new URL(self.location.origin + event.notification.data);

    event.notification.close();

    event.waitUntil(clients.openWindow(urlToOpen)); });

An then, when i click on the notification, I am redirected to the desired URL.然后,当我单击通知时,我被重定向到所需的 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM