Using socket.io with gcm - android

I have a chat website.I am using socket.io in my web site.I need to do an android app for this site.I will use socket.io in app for real time conversation but also I want to add push notification support with gcm.I know the gcm but I need a roadmap.
This is my algorithm:
(In this algorithm,user 1 sends a message to user 2 from web and user 2 uses android app)
1)Get the user 1's message to server
2)Send a ping to user 2 via socket.io
3)If user 2 replies this ping that means user 2 using app right now,send message with socket.io
4)If user 2 doesn't reply this ping that means user 2 was closed the app,server sends a http request to gcm server for send a push notification to user 2.
Is this a good approach ? If you reply is "no" how should be ?

Related

implemented FCM for the Client Project (app on android device). How can I use the Server for sending Notifications

we created a Chat application with a Client and a Server project.
Clients can send messages to the server and the server and the server forwards it to other Clients (like a Chat application should do)
FCM is implemented in the Client project. Users can send Notifications to each other.
How can I use the Server for sending Notifications? Can I just write the Code for sending Notifications in the Server project (Server does HTTP Request with User token etc.).
Idk about that because I think the project should be somehow registered to FCM to use the services.
I only find tutorials about sending Notifications from App to App, not from Server to App.
For example in this scenario:
User installs app, then creates account and gets confirmation email from the server
, then clicks on confirmation link (app is clossed or in background)
, then gets notification that the account is ready
I think this is not possible with using FCM only in the app project

Android: Developing a Chat app

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...

Sending Notification to user when user is offline in android using XMPP

I implemented an Android chat application using Smack client with XMPP server. Everything works fine when the user is online, now when the user is offline (the app is not in running state). I would like to push notification using GCM.
Is there a way to achieve that, and is there a way that we can manage our server itself to take care of that?
I'm facing this problem too.I'm solving this as folowing steps:
Develope a WebService (ASP or PHP or ...) with a database that will contains users FCM-TOKENS per JID
Developing FCM on android devices and send TOKEN whent it refreshed to my WebService and save it in db.
Activate "CallBackOnOffline" plugin in openfire. (that sends POST request with a JSON to defined url when a message received but recipient is UnAvailable)
Set "plugin.callback_on_offline.url" in "Service Properties" to my WebService. So the message and the jid will be sent to my web service.
Now fetch the FCM-TOKEN from my web service db and send a request to FCM containing message body and title.
You can send push notification any time the user is online or not but the device has internet.You have all devices device tokens so when you send a message to a particular user, use his device token and send push notification.

GCM between User to User from same Android App

I would like to implement GCM in my Android app:
One Android app
Registered- U_user and another M_user.
I have Google Sign In in my app thats works fine with JSON and GSON and local MySQL database server with tomcat-server restutil to keep storing Gmail login users.
So, I want to send a notification from U_user to M_user.
U_user does a registration that has been pushed to M_user.
How can I do this?
I am assuming you mean the same Android app installed on different phones (U_user's phone and a separate phone for M_user).
To quote a section in the docs:
To make sure that messages go to the intended user:
Your app server can maintain a mapping between the current user and the registration ID.
The app can then check to ensure that messages it receives match the logged in user.
What this means is that you can send messages to another user through GCM. To let the app server know what to send to M_user, let U_user send an upstream message to your GCM app server (only possible in app servers using XMPP - see here). Alternatively, you have the option to do POST requests to your app server via HttpUrlConnection (can work on both XMPP- and HTTP-based GCM app servers).
Regardless of which method you use, specify in these messages to the server that you would like to send a message to M_user.

Chat algorithm with websockets and gcm

I have a chat web site and I builded an app of this site.Now my site and app using socket.io(websocket).In current version of my app when user closes the app user can't recevie any message(normally).I decided add GCM support to my app for push notifications.But I have questions about this.
My scenario:
User 1 logged into system from pc then he connected to socket.io
server and he is waiting messages.
User 2 logged into system from android app then he connected to
socket.io server.
Now they are chatting with each other.
But 10 minutes later user 2 closed the app and now he won't receive messages.
I am asking what should I do for send push notifications to user 2 ?
I am not asking GCM implemention,I know the GCM implemention.I know the user 2 device id and how can I send push notification to this user.
My problem is:I need to work with socket.io and GCM.How can I detect "okay,user 2 closed the app when user 1 sends a message to user 2 take this message and send to gcm server"
Is this a good approach ? or what do you suggest for like this chat system ? Example you can give an example from Whatsapp how can they handle this problem ? I think they are using Gcm for only push notifications,in app they are using Erlang.
It's not socket.io that connects to the GCM.
You'll need a module for Node.js that enables the connection from the Node.js (running the socket.io) to the GCM. A module like this one gcm module, there are also examples that can get you started.
So, in your case, you'd want to call socket.emit() if the user is online (you should have this already), otherwise the user is offline and you want to push the message by calling gcm.send().
To detect if the user is offline you'd need the to add the user to an offline list when the socket.on('disconnect',function(){//add user to offline list}) handler is called.

Categories

Resources