Tell me more Internet of Things — Part 6— Google Cloud — User input & push subscription
How to push notification to the users using webpush package. Changing the app engine app and adding a separate cloud function
We want our app to recognise interesting or certain limits from the device sensors and the ability to send them as notification to us. As we have only developed a web application we need to have the browser (in our case chrome) listen in the background for any messages from the device. In addition, we won’t have them send from our device directly to us, this will be done in the cloud function. To make things even more complicated we could potentially open the app on different devices which could lead to the users decision to get notified on different devices.
In our implementation we will limit that to one subscription for every registered device. Example — I open the app on my mobile and want to get notified there. Then I open it on my laptop and the subscription will change to the laptop if I activate it there.
Further we may like to get some feedback over the event from the user
For later implementation: This logged data should be used later in an system identification or diagnostic identification to predict or suggest events. This maybe done with simple linear interpolation or up to machine learning of highly nonlinear models.
In our implementation we are creating a new
- Cloud function — which subscribed to the pub/sub from all registered devices. Once the
- Threshold — for a particular device is exceeded the cloud function looks into datastore for the subscription details from this device and pushes a notification via
- Webpush package — to the dedicated service worker of the dedictated browser.
- The app engine — has a new route which once the notification is clicked the user can enter feedback about the event
- The datastore — contains the subscription details and is as mentioned connected to the registered deice KIND from datastore.
- You can subscribe — to a and device event by simply opt in via the chart button.
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
For trigger detection and webpush
Only for better serviceability and in order not to overload the existing pubsub triggered cloud function we create a new one. But we could have used the existing one and extending this function.
This would be our trigger backend function only pushing this notification to the dedicated browser (Chrome) if certain thresholds are surpassed.
The entire concept of notification push delivery is based on service workers implemented in the frontend browser working even in the frontend device without the need to open the browser. Chrome supports this quite some time and we are building on this excellent tutorial
In order not just repeat this tutorial I try to explain the concept with my own words. You might have noticed recently an increase in those tiny notification request which you can either block or allow (depending on your site settings). Once you allow the side to send you notification it can do it without you opening the browser. On a mobile device that would mean you could get all the notification (without having an android app service but just the browser).
Once we click this notification we get redirected to a page where we can leave the events feedback:
So we trigger our notification service by opt in using a button on the chart page. Above the pug code. The only text changes once we disable or enable the service clicking the button.
We could create a seperate subscription KIND (database) as every subscription to every seperate browser on different devices (OS) can get seperate subscription. We just add the subscription details to our registration (tmmiot_device) device kind.
So everytime the browser subscribes to the notification it will override the old subscription details which leads to the new device will get the notification.
We added a new route “/subscribe” for adding the subscription information to the existing KIND for device registration to users.
Inside the cloud function once a threshold is exceeded the function checks whether there is a subscription for this device and if successful send via web-push the notification to this device / browser service worker.
In this implementation it is only possible to have one subscription per device. Which means you could either get the notification on your mobile device or your laptop but not both.
I like your comments, corrections and suggestions 👌.