React-Native: Use Push Notification to wake a background task - javascript

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.

Related

How can I get Expo scheduled (local) notifications to work while Android device is in doze mode?

so I have created a reminder app using React Native and Expo.
I followed the Expo docs which require that you set your app up with Firebase Cloud Messaging even if you're only sending local, scheduled notifications. I also added the exact alarm permission to the android manifest using app.json. Also, the AndroidImportance for the app's notifications channels is set to MAX. Lastly, the Android priority for the notification itself is also set to MAX, as well.
Everything in the app works great, except for one thing:
When the Android device is inactive for a while, the scheduled notifications do not appear at the scheduled time.
I am assuming this is because the device has entered doze mode which is part of Android's battery optimization features. Doze mode can delay notifications.
I am just surprised-- what is the point of a local, scheduled notification feature if it can't even get past doze mode?
I could be overlooking a solution. Does anyone have any ideas? If not, I may have to rewrite a lot of code so that the notifications are not local.
Thanks!
I first tried setting the notification's priority to high, and the notification still wouldn't appear during inactivity. So then I switched the notification's priority to MAX, and that didn't seem to work either.
There is a package on npm for disabling battery optimization in react native apps, but apparently that can get you banned by google, which I'd rather not get banned.
EDIT: One other relevant detail is that when I was at the stage of development early on where I was testing in the Expo Go app, scheduled, local notifications were not affected by doze mode at all. I'd schedule one in the evening, forget about it, and it would come the next morning. I'm wondering if this as an indicator of what needs changing??
So it turns out there are two places where you can set priority to "Notifications.AndroidNotificationPriority.MAX" --
Once when you send the notification, and once when you handle the notification. I had only set it when sending the notification.
For info go to docs Ctrl + F and search for "AndroidNotificationPriority"

PWA push notifications not working when app is not installed

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.

Cordova/Phonegap wake up app on incoming webrtc phone call

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.

React Native JS concern

I am new to react-native world. Following few tutorials, I found that it react-native app runs on javascript engine on mobile. I am following ios tutorial. So when I go to localhost:8081/index.ios.bundle it loads JS for the app. I know I can use ngrok to manipulate this URL.
I have multiple concerns with this:-
If I change the URL of js from something local to something remote, this would result in a completely new app and now I don't need to go through the App Store to upgrade my app version. Can this be a potential issue in the future?
Since anybody can open this JS in a browser which might have important information like client id and secret for the app, can this be an issue as well?
I am not 100% sure. You can indeed update hybrid app easier and if it is only a quick fix you don't need to go through apple store verification process. Apple can also delete your app and ban you if you don't follow guidelines. Usually your js is local in production for instant app start time, and the network is used only for fresh data.
This is solved with an authentication mechanism.

Handle silent push notification in closed react-native app (Android)?

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.

Categories

Resources