Hi, I’m trying to learn how does android browsers subscribe to web notifications in order to show push notifications from webpages
Do you know where I can read about this? Maybe some code for android studio?
My plan is to implement it to the Native Alpha app
A combination of the Web Push API for notifying a service worker and a notification API to display notifications.
You seem to be using the WebView API. If your app already allows for ServiceWorkers to register and execute like they do in a browser, you’ll only need to expose the necessary notification APIs.
If you don’t have ServiceWorkers yet, implement those first. They should be pretty easy to implement based on a quick Google search.
Then you’ll need to expose the notification API. You could do that by injecting Javascript, or by some other means, but the native WebView control won’t have the necessary functionality. In short, I would guess you’ll need to write your own web push client on the native side, expose an API to access that code through WebView bindings, and then inject a piece of Javascript code that’ll redirect browser API calls to your app specific API calls. It’s possible that you can do this without Javascript bindings łand by simply implementing the missing APIs directly) but I wouldn’t know how.
For web push, you’ll probably need to go through Firebase somehow, because that’s basically the only service that can send notifications to Android apps. If you also want to be able to use your app on unGoogled Android, you’ll need to implement a fallback channel somehow.
You will probably need to expose an API and register notification channels for each website loaded into your app, so users can easily manage the notification sounds/visibility through the Android notification settings, set up notifications in your native code, and then set up the correct PendingIntent to handle user interaction. Using notification channels should save you from maintaining your own notification settings; the settings button could simply redirect to the Android notification settings for your app.
Note that notifications can include one or more action buttons, and also have callback handlers that communicate back to their websites for when they’re being dismissed. Android should allow all of those actions through relatively simple native APIs, but you’ll probably need to implement them yourself.
Apple has a pretty good overview of notifications for web apps.
But Native Alpha looks more like a web browser, so I think you need to provide an implementation of the Notification API that uses the Android notification API to display stuff on behalf of the web app.
Does that make sense?
I’ll take a look into those, thanks!