How to send a push notification to pwa from a nodejs server? - push-notification

I have a working PWA application and i have tried integrating push notifications to it which worked. But those notifications happened totally in the front end, a user has to click a button and a push notifications would show up.
In my application i have a cronjob that runs every night to check if certain things are updated. If so i want to send a push notification to a user to notify them.
But i cannot find any information regarding this subject so I'm thinking it's not possible since the nodejs server doesn't have a connection to the PWA. Is there something I'm overlooking on my approach or is it simply not possible?

Related

Firebase messaging not triggering onResume / onLaunch properly

There are no problems with the code, because if I press on the notification it triggers onResume, however if the app it's killed or the user deletes the notification no callbacks are triggered.
Right now I'm saving notifications locally on an SQL db, so when a user logs in back it shows how many notifications he hasn't read. However, If I can't trigger no callback when he deletes the notification or when the app is killed (Actually when the user logs in back, after a couple of seconds onMessage is called with the notification sent while the app was killed, however this still bothers me 'cause I can't get user's attention with notifications) I can't save them locally and when he logs in back in the app, those notifications that he dismissed won't ever be back.
Is there a way to callback a function when the user dismisses notifications or when my app is killed?
Basically as you know, notifications are designed to be removed by users.
So if you want users to see all the notifications from your service, you should consider with another UX something like notifications tab (Facebook, Instagram has)
And, manage all notifications from the server, set flag if it is been activated or not.
As far as I know, there is no callback when the user dismissed the one.
I hope it can help you to decide your service design.

Web Push. fetching web pushes sent while offline?

I am coding a vue.js app using web pushes with Firebase Cloud Messaging and I wondered if it was possible to send a web push to a user and in case the user was offline, to somehow store it for later display when the user opens the app again. Is there a principled approach to this problem, i.e. managing web pushes when the end user is offline?
The reason I am asking this is that, so far, all the web push notifications I've committed to FCM server with tokens of offline clients (i.e. desktop browsers) went into oblivion. To be sure, FCM didn't try pushing the notification again when the clients went back online.
For this reason I am considering coding a self-made dispatcher to manage web push for offline clients, but I need to make sure my efforts are worth it.
updated: I am now able to display notifications sent to an offline client after coming back online using appropriate time_to_live values. However, only the latest notification is displayed. How is there any specific reason why?
FCM's default behavior is exactly like that. From the docs:
If the device is not connected to FCM, the message is stored until a connection is established (again respecting the collapse key rules). When a connection is established, FCM delivers all pending messages to the device.

Web app fetching background geolocation using service worker and push notification

I am looking for a solution to fetch geolocation periodically (every 1 minute) on mobile browser. I did some research and came to know that fetching geolocation in background (when phone is locked or webapp not active) is impossible. In foreground it works ok.
I am making a web app where I fetch user location and send it to my server. All works ok, until the point where user locks his/her phone. I tried many things for workarounds:
setInterval to get geolocation, works fine in foreground but fails in background
converted my web app into Android using cordova, but same problem existed
used https://github.com/mauron85/cordova-plugin-background-geolocation this plugin for android
works good in both foreground and background
but the same plugin does not work in iPhone (I did not tested it, I searched in Google before moving ahead)
Its not good to release my app with just one platform (android) support, thus this workaround also failed for me.
I also considered using serviceworkers for my web app but it seems issue persist for background
https://github.com/w3c/ServiceWorker/issues/745
I have another solution in mind, but before investing time in it, I wish to know if someone has tried it already.
I learnt about Push notifications:
Web app client registers to Push notification
It sends
subscriber object to my server
My server using this object sends
message to Push notification server
Push notification server
sends messages to my web app client
My web app client wakes up my
service worker for a brief period to show the notification message
As per google documentation:
Note: In the current implementation
of Chrome, whenever we receive a push message and we don't have our
site visible in the browser we must display a notification. That is,
we can't do it silently without the user knowing. If we don't
display a notification the browser automatically creates one to let
the user know that the app is doing work in the background.
I won't mind showing a permanent notification to users until my app is running. I am ready for this trade-off.
Now, my question is, at step 5, when I wake up the service worker **is there a chance I call my main.js (main web app) which might be running in background **, will fetch the geolocation and update it to my server?
Has someone tried this solution already with success or failure, please inform.
Can you try Page visibility Api, which listens for visibilitychange of a browser tab is hidden or switched to other tab (in case of browser). For mobiles, it's just a try.
src : https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
or You can look into this answer too..
Detecting when screen is locked

Notification when app is closed in Ionic3

I am building my app using Ionic3 and Firebase. Here users can send eatch other messages. I want when a use sends a message to another ures he/she should get a notification in the phone even when the app is closed. I have no idea how to achieve this with Ionic3.
Any suggesions please.
I can see there are mainly 2 plugins for push notifications. cordova-plugin-firebase and cordova-plugin-fcm
At least I can fine some tutorial for cordova-plugin-fcm in the internet. There is no clear example of any wayout given for cordova-plugin-firebase.
You have to use the cordova-plugin-firebase and each time a user logs in using a device, a unique device ID will be registered with respect to that device. This device ID is used to send notifications. Make sure to handle this (device ID), every time a user logs in & logs out. If you do not remove the device_id from the database when a user logs out, he will continue to receive notifications.
You have to use a cordova-plugin for sending push notifications. One of the many available plugins is: cordova-plugin-firebase. This one is a good fit for Ionic 3 because there is an ionic-native wrapper available.
For sending the push notification you can use a third party service (there is also one offered by ionic) or you can send them yourself from your backend.

Synchronize application with firebase post push notification when application is suspended

I am in the beginning stages of developing a react native mobile application. I wanted to get insights on the following on a conceptual level relating to the below.
Say the application goes into the suspended state (as mentioned here) at which point there is no application code being executed. Subsequently, the server sends a push notification based on some changes to the data. It seems that (at least on IOS) there is a way to use push notifications to initiate a download in the background. My questions are:
Is there a way to initiate this download in react native post a push notification when application is suspended?
Is there a way to synchronize the Firebase data with the application post such a push notification while the application is running in background if (1) above is possible?
Any hints on these will be of great help. Thank you.

Resources