Make Web push notification work using webview in a

2020-08-16 02:34发布

I have a website which is responsive and push notification feature enabled. In my native android app, if i load my website in a WebView, then the pop up which usually comes in chrome/firefox to ask if user wants to allow push notification or not, doesn't shows up in WebView. And obviously the WebView won't support the same web push notification for the app. I know how to implement GCM or FCM in app to enable mobile push, but i wanted to know if it possible just to club the web push notification with android webview so that it would work in my app as it is working in chrome/firefox etc.

What I tried: After digging deeper into this, I finally concluded that as android WebView doesn't support Push Api (with the help of caniuse.com), clubbing of web push with mobile push using WebView is not possible as of now. Am I right? Or is there any app which already does this? Is it possible somehow?

2条回答
劫难
2楼-- · 2020-08-16 03:26

Ow, of course this is possible! I easily implemented push web notifications from one signal in my webview App using crosswalk.

Uses crosswalk, extends Main activity to XWalkactivity Put this on on create:

OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

Yeah, you will have web push notifications working!!!

查看更多
一夜七次
3楼-- · 2020-08-16 03:37

As I'm aware, it's not possible using only WebView .

The solution that worked for me was to find whether the website you want to display in the WebView uses WebSockets. If yes, the notification will probably be available in the according WebSocket communication. The workflow is following:

  1. Determine WebSocket on the website. You can use Firefox "Inspect Code" and in "Network" tab filter out only WebSocket entries.

  2. You can test connection to ws via various tools. Simple-WebSocket-Client for Firefox or command line tool. Simply provide URI to your WebSocket (ws://domain.example/ws).

  3. Implement WebSocket client in your application. There's been different posts on how to do that. I've used this lib to make it nice and clean.

  4. Now you are receiving messages from server, such as notifications.

  5. Parse them and do whatever you want.

查看更多
登录 后发表回答