How to use service workers in Cordova Android app?

2019-03-09 19:14发布

问题:

I want to use a service worker in my Cordova Android add as a convenient way to sync some images to and from the server. I notice that this question states that this is not possible, since service workers must be loaded via https:, whereas Cordova will load files via file:.

On the other hand, it seems that ionic supports service workers. That implies that they have figured out some way to make this work. However, I am not using ionic. There appears to be a cordova plugin for service workers for iOS, but as I said I am on Android.

What is the current best practices for using service workers in Cordova Android apps, if that's possible at all?

回答1:

After digging deep into this issue, I conclude that there is no support for Service workers in Android as it blocks service workers from HTTP or file protocols.

Also the support of Service worker in Ionic framework do not clearly state that it is not supported in hybrid mobile apps. It's kind of misleading too as in this case. Ionic's Service Worker support comes into picture only in case of Progressive Web App and not in hybrid mobile app as mentioned in their official blog

Adding to the above info, most of the functionality that can be achieved by using Service Workers are already available as part of plugins like push notification plugin which should be suffice in most cases.

The bottom line is that the Service Workers are not supported in Cordova Android as well as in Ionic framework. For more info, check out the following link