Implementing push notification in a chat app - firebase-realtime-database

I am building a chat feature using firebase Realtime database where an integer variable stores a number (int online) which when a chat is opened by person A it adds 1 to the database and then if another person B comes online and opens the chat then it adds another 1. So, if the number is 2 I know that both are online and active in the chat app so no need to send push notification.
But the issue that I am facing now is that if a user closes the app without pressing the in-built back button of the chat app (which when pressed subtracts 1 and becomes 0 when both users are inactive), then the number in the database is not changed which causes error in the push notification logic as when Person A sends a message, the code assumes Person B is online. How could this be solved? Please someone help me understand how push notifications generally work?

First off all sending push notifications is completely FREE. The only costs are made by the the backend (cloud functions) that executes the code to send them.
Secondly. If your users are in the app no push notifications will be shown. Even if you would like to show some in your app you would need to handle the onMessageReceived to show something to the user while he is in the app.
That means there is no need for you to do the thing you try to do. Just send all messages as if no user is in the app. It would even cost you more to check for each user if he is online or not and send the messages only if he is offline than just to send them to everyone.
I would recommend to read the docs a little bit to get a deeper understanding of FCM and if you have any specific questions we can all help you with it :)

Related

how to implement "istyping" for a messenger app in swift?

How can I implement the "isTyping" in my iOS messenger app?
My idea:
Consider we have 2 users.
when user1 starts typing the client app tells this to the server.
A silent push notification is sent to user2
This notification makes user2 know that user1 is typing.
When user1 stops typing it tells to the server and another silent push notification is sent to make user2 know about this change.
Problems and questions about this idea
silent push notifications have low priority
Is it a good idea to send several push notifications to do this
When I disabled push notification from Telegram iOS app it kept alerting about "isTypping", so is there a better way to do this?
You can use Firebase to store only the istyping information between users.
Since Firebase allows you to observe properties in real time, you can change your views whenever the value of this property changes.
You can have one DateTime field in your user table. Let say lastTypedCharacter and store DateTime and update it every second when user A is typing anything. When showing to the other user B match the lastTypedCharacter DateTime with the current time if its less than 5 seconds then you can assume that the user A is no longer typing

IOS/Android app custom push notification handling

I want to develop an app, but before I actually start developing, I've been doing some research so that i can be sure it's going to work in the way I'd like to do this.
You can imagine the app as a kind of news app, where the user can indicate whether he wants to receive push notifications, and may also indicate that he only wants to receive a push notification if it is in X distance from his current location.
And this is probally a problem on IOS, On android it would be no problem if a push notification came in to read the current position of the user and settings and then show the push notification or not.
As far as I've read this is a problem on iOS, the system receives the push and the app can not respond to it unless the user clicks on it.
Theres also another problem about closing an app on IOS, ideally a app should not be closed (swiped out) by the user because this would be a force close on IOS.
From ive seen most users still swipe out apps, and this would mean that my app cannot run background tasks anymore.
This is what i thougt about:
Send Silent push, app download data on the background -> Check if this meets the user settings, if so show a local notification. (30 sec time to handle, but do not know if it is possible to throw a local push here too.)
The app sends data on the background over the user's current position before sending a push, the server checks for which apps it should be sent (actually no solution, too much data usage as it may be that the user is only one Specific location, need a good server if the app is used on thousands of devices.).
Does anyone have any idea how to handle this problem?

Parse.com - Send message from Parse.com to iOS app, not like push notification

I'm developing new iOS app using Parse.com, which uses to chat between two users. What is my problem is, when User A sends message to User B, it will store in Parse.com custom class object. But I want to know, how the Parse.com let to know the User B about he received message from User A. We can fetch from User B app with some frequent time. But this is not feasible way.
So, Is there any option that parse.com will send message to specific user who received message recently? I don't want to user Push notification for this, because push notification is different concept and also not reliable.
Thanks,
Vijay.
What you are asking for is exactly what Push Notifications are for. Your only other option is polling for changes.
If you find Push Notifications unreliable you would need to implement a combination of push + polling.
There are some other technologies out there, but they're not options with Parse without a lot of middle-ware.

iOS Parse Chat App: Updating data from multiple pushes while in background/inactive.

I'm implementing an iOS chat app using Parse. Currently, most chat data is stored on the user's device.
When you send a push from one user to another, and the receiving user is on the app, the device will save the message along with its data (who it's from, when sent, etc). From there, if the receiving user is chatting with the sending user, their chat dialog will update. If they are still using the app, but not chatting with that user at the moment, a popup will display with the message.
This works great for when they are in the app.
But when they are outside of the app, a push will come through with the data (message, who sent it, when sent, etc), and a push notification will popup on the user's screen. But when they open up the app, I am not sure how to transfer that data from the push to the app's storage.
I guess I do understand how to do it (if there was only ONE push notification AND the user opened the app by tapping the push notification: there is a delegate method that seems to have a dictionary of the push notification's data).
However, in the case of there being multiple messages being sent before the user opens the app, and/or the user opens the app using the app icon (and not by opening the app from the notification), I am not sure how to access this data. It would seem like there would be some sort of way to get an array of dictionary objects, but I'm not sure.
I know I could implement a way to store the data on Parse, and then delete it when it has been downloaded, but if I don't have to do it that way, I wouldn't want to.
Any help would be much appreciated :)
You'll need to store the data in Parse and allow them to query all messages since they last checked.
I would suggest using a counter every time a message is saved, that way in the receiver you can check if that counter has only gone up by one compared to a local copy of the counter, which means you can just use the message in the push notification. If it has gone up by more than one then you know you have to ask Parse for the rest of the messages.
You can have Cloud Code that each participant in the chat calls to say what number they're up to... then you can delete anything with a value less than the lowest of all the participants. You could even integrate this logic into the getMessagesSince(counterValue) call.
Note that in this case you would also want a gotMessageFromPush(counterValue) to let the server know that you got a message via Push.

Regional Monitoring and Push notifications in iOS

I need to update my users for things that happened around their current location while the app is in the background.
To my understanding:
If my server sends a Push Notification to a client, the client would immediately show that message.
I can set up the app so that there is a specific location, with a given radius could fire a message.
So what I want to understand if it is even possible to update my users about things that are new in their locations.
I was thinking of two possible solutions, I am not sure they are possible.
One, I guess if the Push Notifications would allow a function to run prior to its display,
deciding if the message should appear.
For example: something happened in area x,y - The server initiates a message to all devices. Only the devices within range of x,y and a radius z, would show the message to the users.
Maybe the Regional Monitoring service can send a message to my server and my server can then decide if to send a Push Notification back to the client...
For example
The user has entered a defined location, determined by the app. The app sends a message to the server. The server decides if a message is due.
Any ideas? is that even possible?
Filtering push notifications by topic is something you need to do on the server side, not the client side. Apple specifically states not to send messages to users that aren't relevant and you won't be able to block them when the app isn't running. Additionally, if you are using a service to manage your push notifications you don't want to pay for messages that aren't relevant.
So when you register a device into your database, you should register what topics that person is subscribing to (ie. save a list of topics that user is eligible to receive). Then when the event is triggered that generates the push notification only send to devices that are registered to that topic. I believe a number of push platforms have this capability already built in. On UrbanAirship and Azure Notification Hubs you can use their tags feature. Or you can develop it yourself if you do your own push server.
Take a look at Parse. They have this kind of functionality baked right in, just send the push to clients that match a geoPoint query.

Resources