Modify notification from FCM / onMessageReceived not called when app is in background - javascript

I'm building an app in react native, and I attached react-native-fcm library to the code, in order to work with notification messages.
I have two main problems:
1) Notification from the firebase doesn't look like the ordinary notification messages: it's not appear with app icon, it doesn't popup and presented when the screen is closed.
2) I wanted just to get the event of messaging, also here after reviewing the library code, I saw that notification get to the function: onMessageReceived only if the app on foreground, like it say in the website.
But in the website I couldn't understand how to get the general event of catching notificaiton regardless to the appstate.
I also tried to implement class that extends FirebaseMessagingService, and there the notification doesn't get to the onMessageReceived override at all.

You will need to use FCM API and send data in data field instead of using Firebase console or notification field so that the method onMessageReceived is always called regardless of whether the app is in foreground or background. When this method is called, you can create a notification as you like.
Read about FCM admin API at: https://firebase.google.com/docs/cloud-messaging/admin/
Read about FCM HTTP API at:
https://firebase.google.com/docs/cloud-messaging/http-server-ref
You can use Postman to send notification if you choose to use FCM HTTP API.

Related

React-Native - How is it possible that when I close an app like messenger I can still receive incoming call notifications from others

I am building an app like Messenger, everything works normally like sending messages, receiving messages, video calling. the technologies i use are react-native, reactJS, socket.io, nodeJS, react-native-webrtc
I wonder how can when I close an app like the messenger I can still receive incoming call notifications from other people. I'm studying firebase notification and react-native-background-timer but it doesn't seem to work the way I want it to
Has anyone had a problem like this and have an answer, please let me know
Have a nice day guys <3
In react native firebase there are two types of notifications "background" and "foreground".
Foreground Notifications:
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.
When you use a foreground service, you must display a notification so that users are actively aware that the service is running. This notification cannot be dismissed unless the service is either stopped or removed from the foreground.
Background Notification:
A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.
You want to follow there Documentation to apply push notifications in react native.

Service Worker onClick event - Open and pass some data to a PWA

I have implemented push notifications for my PWA and my service worker (SW) is currently handling clicks on a notifications like this:
If the app is running it will put it in the foreground and send it some data using the Client.postMessage API
If the app is not running it will launch it via Client.openWindow using URL parameters that contain some minimal information about the push notification
Point 1 works just as I want. But for 2, I would like to be able to send to my app the same data as for 1, and I cannot add it to the URL (the payload is too large). The issue is that I can only use Client.postMessage when my app is ready to receive such messages...
What is the simplest way to do that?
Note: my use case is a chat app for which a push notification is displayed when a message is received and a click on the notification triggers the app and shows the corresponding chat and message. I want to pass the content of the message itself (which is in the push notification received by the service worker) to the app so it can show directly, instead of having to pull it from the backend.
This is how I solved the problem:
SW receives a push notification and the user clicks on it
SW stores the information to send to the app in IndexedDB associated with a unique id
SW uses Client.openWindow with a custom URL + the unique id to open the app
When the app is started, it pulls the data from IndexedDB using the id

Firebase Firestore / React Native Chat Notification

My app currently supports a chat function which is working only with Firebase Firestore, nothing else.
My idea is to implement a notification whenever a new message is received, the catch is: I want to get the notification even if the app is closed. (If you close Instagram or Whatsapp you also get notifications if somebody messages you, that's what I mean.)
The problem I face is that all Firebase listener work only as long as my app is connected to the Firebase servers but as soon as I close the app of course the app disconnects from the servers and so do the listeners not work anymore.
What I need is a function or a feature to track in the background if something on the Firebase Firestore has changed (in this case, if a new documents in a specific location was created) and if yes, trigger a notification, even if the app is closed.
Does anybody have an approach for me how I could accomplish this task?
You can use Firebase cloud messaging to solve this problem as whenever there is new data available to sync it checks with the client device status and updates it. Here you can find step by step guide on how to set it up.

Listen to firebase database changes when app is closed

I am creating an Ionic 2 app with firebase and I need a way to listen to database changes (specifically on child_added) when the app is closed (I.e. in foreground,background and killed)
Basically, I want to use WebRTC to make calls within the app like whatsapp and I am following this post - https://websitebeaver.com/insanely-simple-webrtc-video-chat-using-firebase-with-codepen-demo
However, the only thing that puzzles me is how it will work when the app is closed. Can anyone please help me understand?
Thanks!
It's not possible to actively listen to database changes using the Firebase client SDK in exactly the same way that you can when your app's code is running.
If you want your app to receive information about changes to your database, you can instead use Firebase Cloud Messaging to send your app a notification with a small payload that contains information about the change. When your app receives the notification, it can then make a decision about what to do. There are some limitations with web support, so be sure to read about that.
Also look into Cloud Functions for Firebase to make it easier to write some server side code that can trigger in response to a database change and send a notification when those changes happen.

Is there any way to create channels in Parse.com via REST API?

I'm working in a Ionic App to report potholes in the city. The user can make reports through the App or the Webpage, then other users can comment this issue and receive notifications about this specific topic.
I read the documentation for Parse.com of how to send push notifications for specific channels, for example, each report is a different channel. So if I comment in the report 123214 so automatically I will subscribe to this channel in Parse.com and receive notifications. Already know how to do this.
But Is there any way of create these channels in the same time that I generate the report in the App using javascript and REST API request ?
best regards

Categories