I am developing an one-to-one chat app. The problem is how should I keep all clients and server in sync? There can be multiple cases when the message fails to send from the client, or it is not received by the client.
Right now, I am using a pub sub service and GCM. Whenever user is using the app, I subscribe to a unique channel to receive messages and use api to send message. Also, through pubsub I am maintaining user online status.
Whenever app goes into background, pubsub disconnects and user goes offline. In this case GCM is used to deliver messages to the client.
This systems is working fine. But rarely there are messages that are not delivered to the client.
Can this be improved to assure message delivery ?
Here is a good tutorial.
with client side and server side codes...
Related
I have a script on server which sends sms or telegram messages to users. I want to send sms when user is offline and send telegram message when user is online.
Is there any way to detect whether android user is online or not from my server (maybe sending request to already running some app?).
Note, users are willing to share some credentials if necessary.
Android OS version: Api 21+
Well, I created my own app.:
I used firebase cloud messaging to send notification (as it is lightweight and based on event-driven model) to devices from my app server. Byw, telegram uses gcm which is younger brother of fcm.
I send only data messages (only payload) to my app from my app server so that I can get my app notified about notifications even if it is in background.
When app receives notification, it sends upstream message so server knows that user has received message.
If server doesn't receive response from user within 5-10 seconds, then user is most likely to be offline.
I have two version from my app one for the customer and one for the client I need to send push notification to the specific client(in client app) when the customer take an specific action (in customer app).
I know that I should use Firebase Cloud Messaging as it's a new replacement to GCM, but I can't figure out how I can do this in both server side and client side.
you have a lot of options here!
let's start with the server side:
you have two options HTTP or XMPP, HTTP is a one way connection XMPP is two way.
for your app you will need XMPP for upstream and down stream messages
check there Guides it's really great: Server guide
there is also a Java and Node.JS Admin SDKs but i don't know much about.
but there is a great Tutorial which describes the whole proccess if you go for Node.js
So what happens is:
when a device sends an upstream message
Firebase will forward it to your server
Your server should handle the message and send it to the other device through Firebase cloud messaging
you can implement a server-less app using real time databse but you will not receive message in background, but when the receiver app is running it listen to databse changes and send notification to user
you can also make a server listens to databse changes "instead of receiver" and take actions upon it"send a message to the other device"
Edit:
with the new cloud function feature you can now use it to send notifications to users based on database events,
so you can add your message to database in a class
and make a cloud function to listen to this class write events
and whenever a message saved to this class, it sends it to desired
receiver
so now you don't need an external server to send notifications to
devices
check this cloud function sample, it's an example of sending notifications based on database event.
According to the Firebase Cloud Messaging documentation if the app server sets a field "delivery_receipt_requested" in the upstream message, it will receive a delivery confirmation when the app confirms receipt. However it works only if app server is connected with XMPP connection server. It will not work for HTTP connection server.
I'm very new to GCM/FCM and haven't setup my server yet. Hence am working with Firebase console to test my app for notification receipts.
So, following are my queries.
Will the field "delivery_receipt_requested" be sent to the app or the connection server itself will consume it.?
If the app receives this field in a notification will it need to act differently from the notification which do not contain this field.?
Any references to documentation or example implementation which makes use of this field is highly appreciated.
Thanks in advance.
Will the field "delivery_receipt_requested" be sent to the app or the connection server itself will consume it.?
The FCM client installed in Google Play services will handle this.
The application will not notice anything different.
If the app receives this field in a notification will it need to act differently from the notification which do not contain this field.?
The application doesn't need to take any action. indeed the app will not receive this field.
Longer explanation:
delivery_receipt is not exactly a confirmation that the app received the message, but it's a confirmation that the device received the message. To be clear it's still possible that the device crashed or run out of battery while the message is being passed to the app.
this feature is implemented outside of the application. so the application doesn't need to be aware that this message requires a delivery receipt
you can implement a more precise "application delivery receipt" by calling your backend in the method that receives the message.
(like an http call to: http://mywebsite.com/confirm-msg-processed-successfully.php?id=133)
I want to implement my own push notification server, more than a need for the product I want to know How it can be implemented.
I read documents for XMPP servers like tigase, ejabberd and openfire. I know that XMPP servers can handle online messages really straight forward, just like sending notifications as chat messages. Here is what I think is true:
Hypothesis: Having an OTT (i.e. Telegram), you can send notifications or chat messages by the XMPP client (the mobile app) and an XMPP server (i.e. ejabberd), or think of a pub/sub app that gets server status changes on a mobile device is using ejabberd as XMPP server and another XMPP client as an app on mobile.
My question is, if the hypothesis that I mentioned above is right, a push notification server would be a message queuing server to queue Pub/Sub messages for offline users and an XMPP server to send notifications/messages to online users.
Is it right? I can have my own GCM-like server by having a message broker to handle the queues and an XMPP server to handle message sending?
You can definitely use an XMPP server like ejabberd to implement your own push notification service on Android. You need to have an application that stays constantly connected and will use the local Android API to display notification on the mobile when receiving a message.
On Android, receiving a notification is clearly separated from displaying it. It means you can receive it by any mean you like, but still display it as a standard notifications, even if it did not come to your device through GCM
Our organization has an Android app and an iOS app.
We want to start pushing notification to these apps.
Android has GCM.
Apple has APNS.
But we want to create an API which will work on both android and iOS.
What is the easiest way to setup a server so that when a push notification needs to be sent, it knows exactly which server to send the message to?
I use a service called Parse to do my notification pushes to both Android and iOS. They have great documentation and libraries available. You can get some details here: https://parse.com/products/push
As a little background this is for a university setting where multiple colleges apps as well as distance education may be using the service. Here is the approach that we are using in our organization. If you look at the way APNS works it can be used by just sending a web call to the APNS service with the token id. GCM is very close to the same type of system. Basically create a JSON package and send it to the desired service.
Here is our steps we used to create this service.
Server admins created a server and database that can be called that will collect the tokens from both android and ios devices. When the device registers we also send what type of device it is. This is possible since we are just sending data to the database that is has been created.
From here we then created a couple of python scripts that send the data do the desired service whether it is ios or android. These scripts gather the appropriate data from the database and sends the packaged data (JSON package) to APNS for ios message and GCM for google cloud.
We also created a web interface so that those who need to send messages to the devices can.
The rest of the implementation is up to you to decide the best way to utilize the service. For example when to check for invalid devices,
Because we are planning on using this same server for multiple applications we can send the type of device, token, application, or whatever else is needed for an application to distinguish it from others we produce so that each application that wants to use the service can. I hope this helps and gives you some idea on how to accomplish this.
For APNS, Maybe you may consider this forked version of PyAPNS that has enhanced message support.
https://github.com/jimhorng/PyAPNs
which means it will catch error response for failure messages and resent the message which are discarded by APNS while sending between failure messages and receiving error response.
Solution:
Non-blocking ssl socket connection to send notification without waiting for response.
A separate thread for constantly checking error-response from read connection.
A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns.
(Reference to non-blocking apns pull request by minorblend, enhanced message by hagino3000)
Result:
Send notification at throughput of 1000/secs
In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one.
For GCM, you may consider https://github.com/geeknam/python-gcm
For generic wrapper that support both or more mobile provider:
https://github.com/Redth/PushSharp