Push notification to Android phone
1. Configure Firebase.
This is huge project and I can not explain all parts of this project, this is description of one small part of this project, related to Android phone.
As usually with Android we started from Firebase project, this is little bit strange because I will not use Firebase as storage, but Google use Firebase console for push notification.
But, because I want to create log of message in Firebase level, and log functionality required BigQuery functional, I need to link BigQuery to my project.
2. Make test messaging service project
So, all we need from Firebase - two files: google-service.json and secret.key. And now we can start new project in Android studio with registered in Firebase name.
Because goal of this project is push notification to my phone, I need o understand FCM_TOKEN (unique ID of my phone), of course in other case we need to store this phone ID to database, but this is not applicable to current project. For current project I select more simple way. Add Firebase Messaging dependencies to project and in the main form add simple code inside onCreate() of MainActivity.java:
This allow me deploy application to my phone:
# cd /home/admin/Android/Sdk/platform-tools Firstly check That Phone Is Connected # ./adb devices
If this is OK, going ahead, if no Fixing-ADB-Access-on-Linux.pdf
Build and Install via Gradle # cd /home/admin/AndroidStudioProjects/GewerbeNotification # ./gradlew installDebug Launch the App on Real Phone # ./adb shell am start -n com.vbnet.gewerbenotification/.MainActivity Or just select device as target device in Android studio
Than I touch long key, FCM_TOKEN of my phone, this token need me for next parts of project, I use this code in FCM_Service.
3. Configure Supabase.
I will use Supabase as main storage (and Use Raspberry PI Zero 2W as fallback server for Supabase), so, I need to create Supabase project and add needed table.
Main idea of configuration Supabase is call webhook when new record appears in database. This is my final structure:
For protect Webhook from rogue request I add secret key to Webhook header.
For common use this is enough, but unfortunately not in this project, because notification is important and Google can miss notification in some case I want to make log in Supabase from WebHook. This require supabase secret key/service_role key.
That's it for Supabase. From Supabase we need:
- Project URL - we cn see it in URL of our account
- API key for protect Webhook and, of course trigger with Webhook
- Public key for read public data. Of cource we need configured policy to allow data reading from our application (in first step reading for anybody)
- Secret key for store log of each Webhook, this s protection if Google for some reason don't push notification
4. Configure Cloudflare worker for Webhook.
My webhook I place on Cloudflare worker. This worker will handle Webhook from Supabase and push notificationto Firebase and write to Supabase log for each webhook from Supabase.
All data that we take from Supabase and Firebase we prepared for this worker, all this data I pushed as secret to supabase worker.
npx wrangler secret put SUPABASE_URL npx wrangler secret put SUPABASE_KEY npx wrangler secret put FRONTEND_API_KEY npx wrangler secret put FRONTEND_DOMAIN npx wrangler secret put SUPABASE_WEBHOOK_KEY
Full code of this project I have uploaded to https://github.com/Alex-1367/supabase-webhook-cloudflareWorker.
5. Final checking android application.
Goal for Android application is not only a service for pushing message as handle Firebase request, but looking for Supabase Db for reading all request.
Full code of Android application I have published to https://github.com/Alex-1367/AndroidNotification
6. Configure Android phone to show notification.
There are some setting in Android application to allow push notification. We need add service to autostart and allow pushing notification.
And, as you can see, application working perfectly.
6. Configure Cloudflare worker proxy server for frontend.
What missing in whole scheme above? In whole scheme I don't publish how exactly placed data to Supabase. Because, firstly this is specific of my current project, and secondary this is sophisticated and complex multi-layer software, assembled from proxy server, service for partner, worker for social networks and many other different components. Commonly, all components can placed data to supabase only through Cloudfare proxy server looking as proxy server in this project Site-leech based on Cloudflare, but proxy server for this project is more sophisticated.
Proxy server for this project can store image for R2 storage, you can see that R2 storage bounded to this worker:
This worker require right for access to R2 Cloudflare worker.
When data image to R2 referenced to Image placed to Supabase.
7. Configure Cloudflare worker for frontend.
As I mention, I can not publish my frontend code, because this is most sophisticated and multi-layer software, but all variants of frontend used my proxy server, because proxy server maintains the integrity of Supabase BD.
But best important news, that Cloudflare directly support Angular project, as I mention, I can not publish my final frontend code, but you can take a look how basic Frontend Angular project for Cloudflare worker looks https://github.com/Alex-1367/BasicAngularFrontendForCloudflareWorker
Android context:
)
|
|