We have a pretty common usecase with our (via Capacitor) Android generated app:
Once it receives a message via e.g. a Websocket (or third party apps like OneSignal, Firebase etc) we want to bring the app to the foreground in case the user is currently interacting with other apps (like Skype, Whatsapp etc). The reason is that we have implemented an "Alarm" scenario and if an alarm comes in, the app should come to front and show what's going on. Simple push notifications won't do the job here.
So we researched on the topic but as we're not native Android devs, we don't understand the full picture clearly.
e.g. Android bring app to foreground on Firebase notification suggests that via FLAG_ACTIVITY_REORDER_TO_FRONT it is possible to bring an app from the background to foreground.
The question is how is this going to be implemented in a hybrid app scenario (like with Cordova/IONIC/Capacitor).
In our app we are pretty far to listen to API signals via Websocket. Once an alarm is received we are able to send a signal to the App via Websocket so we can do pretty much anything. We could for example redirect the signal from the webapp back to the app container.
The question now is how can this scenario be solved either via Websocket or Firebase (FCM) and is it possible to solve it straight through the IONIC architecture?
In a cordova/ionic hybrid app I would use the cordova background mode plugin and the window_system_alert permission plugin. Those two are what I'm using and its working like a charm.
The steps I'd follow:
The first one was to include the force-start to the notification body
The second one was to give permissions to the app to be drawn over other apps. I managed to to this using this plugin: https://github.com/wryel/cordova-plugin-system-alert-window-permission. It requests the SYSTEM_ALERT_WINDOW permission.
The last one is to install the background-mode plugin (https://github.com/katzer/cordova-plugin-background-mode) and whenever you receive a notification you wake up the terminal and show your app in foreground (enable -> unlock -> moveToForeground).
Please, let me know if I misunderstood something and I can change my answer.
Integrated Applozic in android app, and also tried sample app,
notification for any message only works if app is open or in background.
It does not work if we close/kill application (remove from background).
All the setup for firebase key etc is done exactly as given in doc.
And it is correct I think as notification works if app is open.
I think service which works to show notification stops when we close app, How to keep it running?
Thanks
I'm testing Firebase Push notifications, sending a notification from the Firebase composer panel, and I noticed that if I close the app process from App Information panel, the push notifications sent doesn't reach the device. Even if I start again the app the notification is lost and is never received.
I also tryed this:
close the app process -> shut down the device -> power on the device -> send a notification... and the notification is not received!
It seems that firebase can only receive notifications if the device has the app started and not 100% closed, I mean, closing it just with back key but not killing the app process.
How is this possible? It is supossed that firebase should receive notifications even with the app closed.
I'm testing on a Nexus 5X with Android 8.0 and I'm using the last version of Firebase push Notifications.
Sorry for the late, but hope this help next users that will have this problem because there is no answer selected as "Solution".
When setup correctly the service, this will work even the app is closed. That because, Firebase Messages travel by Google Play Services so closing your app doesn't have a relation with the service.
At first, notification never came. By searching in the device settings I saw that the energy saving system for my app was active (when closed was removed from stack) so notification was sent but my app couldn't take and display these.
After disabling that option, I've test many time and I found that sometimes notification come with a late of 2-3 minutes when app is completely closed. Sometimes it touch the 5 minutes. You need to be patient and it will come!
Instead, When app is opened or closed simply by back button, notification come in few seconds.
In your AndroidManifest.xml file remove android:exported=false from your Messaging service.
Explanation: When your app is completely killed or removed from back stack. OS tries to restart the messaging service but if there is android:exported=false in your manifest file then OS will not able to restart the service because such service can only be restarted by the same app.
Reference: https://developer.android.com/guide/topics/manifest/service-element#exported
It seems that firebase can only receive notifications if the device
has the app started and not 100% closed, I mean, closing it just with
back key but not killing the app process.
No, FCMs are sent to all the devices that have Google Play services and the targeted application. That is why it is called Push Notifications.
Your application also get notifications when it is running, to handle those you need to override
onMessageRecieved(RemoteMessage mes);
There could be many reasons for the app not getting notifications. Some of them could be :
Messaging Services not included in the Manifest
Play services not configured correctly. Or not present in the Phone.
Sometimes Latency is High (rarely). I noticed it sometimes take take about 2-3 minutes
after composing.
SHA1 fingerprint not registered in Console and/or updated google-services.json not present in sources.
Uninstall and reinstall the app. So that token Regeneration may take place.
Please follow this link to get started with messaging.
https://firebase.google.com/docs/cloud-messaging/android/client
Have you added firebase services on Java code?
Here is the link: firebase/quickstart-android
You have to add those 3 java file in java folder and also add those service name in AndroidManifest.xml
if you are sending it from your firebase console it sends a notification message so those you will not get if your app is closed, you need to send messages that have the data payload which the console does not do.
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Took this screenshot from Firebase documentation, seems its not possible with FCM
I want to create a service whose function will not be known forehand. When the app is started, the function (which is decided at run time) to be performed is sent from the server to the mobile.
For example, when the app runs server may ask to list all installed apps in the mobile and app replies. When developing the app we dont know that server is going to ask for installed apps. App should be able to respond to any command from the server.
Android app acts like a client to all commands from the server.
Apply the push notification, while on receive the notification, start your service with the tasks sent by the server.
I have implemented a push notifications plugin for PhoneGap Build, waited for it for so long
https://build.phonegap.com/blog/introducing-genericpush-plugin
However I'm having issues with the Android app not running in the background. Push notifications are not received when I close the app. Everything is fine when the app is running. Has anyone tried it with Android? Are there any fixes that need to be done?
Thanks!
It's not something related to the GenericPush plugin itself.
It's something related to the way Android manages apps. If your user manually closes the app, it means that he doesn't want to receive messages anymore. This is why even the GCM BroadcastReceiver is stopped and the device doesn't receive the notifications.
Here's some useful links:
Android GCM google groups
Similar question, related only to Android (native)