在 angularjs 中使用 Service Worker [英] Using service worker in angularjs

查看:33
本文介绍了在 angularjs 中使用 Service Worker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在我们的 angularjs 应用程序的 service worker 中触发事件.

How we can get event fired in service worker in our angularjs app.

这是在 chrome/firefox 中工作并显示通知的示例代码

Here is sample code which is working and showing notification in chrome/firefox

self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
// console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);

console.log(event.data);
console.log(event.data.json());
console.log(typeof (event.data.json()));
console.log(event);

window.dispatchEvent( new Event('dataisthere') );

const title = 'YummZ';
const options = {
    body: 'Message Received \n ' + event.data.json().message,
    icon: 'images/icon.png',
    // badge: 'images/badge.png',
    data : event.data.json()
};

event.waitUntil(self.registration.showNotification(title, options));
});

我试图调度一个窗口事件,但出现错误窗口未定义

I tried to dispatch a window event but i got error window is undefined

当 Service Worker 收到推送通知时,我需要通知我的 angular 应用程序执行操作.

when service worker get push notification, i need to notify my angular app to perform action.

注意:新来的服务人员

推荐答案

阅读 这个.尚未对其进行测试,但我认为您正在寻找的解决方案的总体思路就在那里.标题说如何在 Service Worker 和客户端之间发送消息,因此如果您设法弥合了这一差距,您几乎可以告诉您的 Service Worker 做您想做的任何事情.

Have a read on this. Havent tested it yet but I think the general idea for the solution you are looking for is there. The title says How to Send Messages Between Service Workers and Clients, so if you manage to bridge that gap, you can pretty much tell your service worker to do whatever you want.

这篇关于在 angularjs 中使用 Service Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆