I'm working on a react-native app that is closed the majority of the time. However, I need to be able to send updates from the backend to different clients so they can update local geofences.
I figured I could use silent push notifications for this (using FCM). This appears to work fine when the app is running (either in the foreground or background), but when the app is closed, I am unable to handle these push notifications.
I'm able to handle normal push notifications while the app is closed, because when the user presses the notification, the app is launched and the notification is available as the initial notification, but this isn't an option with silent notifications.
Is there any way to have my app handle silent push notifications while closed? Either by opening in the background, handling the notification, and closing or by registering some kind of background service?
You may want to take a look at react-native-push-notification#silent
Basically for android:
If your Android app is not running when a silent notification is received then this library will start it. It will be started in the background however, and if the OS starts your app in this way it will not start the react-native lifecycle. This means that if your notification delivery code relies on the react-native lifecycle then it will not get invoked in this situation. You need to structure your app in such a way that PushNotification.configure gets called as a side effect of merely importing the root index.android.js file.
And IOS:
The crucial bit of an iOS silent notification is presence of the "content-available": 1 field.
Related
Currently, I am working on a PWA.
I would like to implement a push notification system, luckily the browser exposes the Push API. I got this part working so far, I can receive the push event in the Service Worker.
But when displaying a notification, the notification is received 'silently' in the background.
What i wish that happened:
But this 'pop-up' style message is disabled by default when the PWA is installed (at least, I think so). The default behaviour causes the notification to only shop op in the notification tray, without ever showing above popup.
When I go the the settings of the PWA's notifications, there is an option to enable pop-up notifications: 'Show as pop-up'
When i enable this options, i get the desired result. However i wish this to be de thefault behaviour, and not have to tell every user of the web app to change the settings on their device.
My used code for the Notification from within my push event listener in the service worker.
self.registration.showNotification('New message', {
body: payload,
});
Testing on Samsung Galaxy S22 with Chrome as the (default) browser.
Does anyone know why this is the default behaviour and if there is a fix for this?
Thanks in advance
Are push notification supposed to work only if the PWA is installed?
If I load the app in android chrome browser, push notifications are not displayed at all (background or having the site opened) even they come thru the wire.
If I install the app, all notifications are displayed properly (background or focused app).
Is this normal a behaviour or something wrong in the implementation?
This is as expected.
The service worker runs on a separate thread than the one used by your application. This is the reason why your web app can still receive and display notifications even if you (or your user) is not currently visiting the web site.
Therefore if the PWA is not installed (meaning the SW is not running on the client side), there is no code waiting for the incoming notifications.
I wrote an article about service workers, if you want to deepen the PWAs topic.
UPDATE
There is an article specifically from OneSignal about not receiving Push Notifications on Android, maybe you can find some hints. If you check the OneSignal dashboard, can you see your client registered there?
[This point is for other users landing to this question] If your browser does not show web notifications, you can verify on "Can I Use" web site that your browser version supports notifications and push API and eventually update it.
I have written an audio/video call hybrid app written with cordova/phonegap. I used
1) webrtc for actual streaming of audio or video
2) server sent events (SSE) to listen for incoming calls. https://apifriends.com/api-streaming/server-sent-events/
3) simple-peer https://github.com/feross/simple-peer
When the app is open, everything goes great, i can receive incoming call signals etc and connect. The problem is that when the app is off. I cannot open app or get any kind of indication of call status.
I decided to do some digging quickly found out that this was not possible from within a webview. I tried some plugins like https://github.com/katzer/cordova-plugin-background-mode which worked great for some time but would quickly drain the battery and your app may be rejected on appstore notices.
When my app is opened, incoming calls will be correctly captured. all i need is to open the app. I read about background push messages (silent) but failed to get these to open app. The push messages handlers were only called when app is in foreground.
So i finally fell on this doc and its the closest i got with this answer. There is no direct support for background service in Phonegap/Cordova apps.
The reason is that your hybrid app is written in JS, and your JS code
runs in an Activity that has a webview. As soon as you exit your app,
the app is suspended.
If you really want to have background service in android, you can
write your Android service in Java (native) and call your service from
your JS code. (Native for background service & JS for your app)
https://forum.ionicframework.com/t/how-to-run-cordova-plugin-in-android-background-service/6677/3
From the above, i gather i would have to do the actual logic of detecting incoming calls from the java service and if i detect anything i can force my application to open. Below is simple logic i do once app is open.
if (documentLoaded && isLoggedIn)
{
//url returns json only if there is an incoming call otherwise empty
var source = new EventSource("https://www.url.com/api/sse/incomingcall.php?userid=" + userid, {
withCredentials: true
});
source.addEventListener("incomingcalls", function (event) {
var callSession = JSON.parse(event.data);
// HandleIncomingCall();
}, false);
}
My issue is that i am not a java coder and havent written a plugin before. how would i create an android service that will listen to above url periodically and if detect any json open my app.
I am creating a small debugging app to monitor a particular interaction (WebRTC) between two users of my main app.
I have a web app (React) and a mobile app (React-Native) that each connect through socket.io to a node server I created. This node server's purpose is to collect information from both users, and display this information in a web page in real time.
My web app has a button that when you click it it opens up that debugging web page in a new tab with the debugging information; I just use window.open. Is it possible to do something similar from my mobile device, running a react-native application? That is, can I click a button and have a browser tab open on my laptop somehow? Does React-Native remote debugging work with something like this?
There is currently no supported way you can do this in React Native.
However, you can certainly add it yourself.
Navigate to node_modules/react-native/local-cli/server/middleware/getDevToolsMiddleware.js
Look for launchChrome(debuggerURL);, you can invoke another launchChrome function with your desired URL.
This will only appear when you select Debug JS Remotely.
You can configure the CLI server code more to launch the URL when you are not debugging.
As far as I know, push notification can be used in react native, even when the app is closed. would it be possible to use push notification to run a background task in react native?
for example, when a push notification is sent to a device, it runs a function to fetch data from server and update database.
https://github.com/zo0r/react-native-push-notification
Push notification and data notification handling part could be done using react-native-firebase easily (I recommend to use react-native-firebase instead of react-native-push-notification because it has wide community support and support for many more firebase services)
And in here it show how to fetch data in background for iOS; with the few modification same thing could be done in android also.
Useful links :
https://rnfirebase.io/messaging/usage
https://www.npmjs.com/package/react-native-background-task
Please have a look at react-native-push-notification#silent
Android:
If your Android app is not running when a silent notification is received then this library will start it. It will be started in the background, however, and if the OS starts your app in this way it will not start the react-native lifecycle. This means that if your notification delivery code relies on the react-native lifecycle then it will not get invoked in this situation. You need to structure your app in such a way that push notification. configure gets called as a side effect of merely importing the root index.android.js file.
iOS:
The crucial bit of an iOS silent notification is the presence of the "content-available": 1 field.