How to notify the user about received GTalk chats when the app is not running

368 Views Asked by At

I implemented a chat client using GTalk servers and (a)Smack library. So far so good. Now, what I can't wrap my head around is how to notify the user of received chats when the app is closed, the same way all other chat apps do. I was reading into push notifications but all examples I find have a server component as well, which I obviously don't since my app is just a client for GTalk.

Should I be using GCM and implement the server side for this? Should I instead attempt to start a service that will listen for incoming messages at boot time (similar to this example)?

3

There are 3 best solutions below

4
Amedeo On BEST ANSWER

Service is definitely the way to go, you can keep it running so that your XMPP connection remains open.

From the server you can receive the messages and through a Broadcast Receiver you can show your notification if the app is closed, when it's opened you disable the previous Broadcast Receiver and you register a new one to manage messages in your activity for example.

This is pretty much my implementation.

Thumbs up appreciated!

Enjoy

5
vitalyster On

You already have service that listens for messages, maintain network connection, etc., and it is Google Messaging Service. You want to reinvent the wheel, and duplicate existing functionality. It makes sense when you are using device without Google or other vendor services, but on typical device your service will be co-exist with Google (Amazon, Nokia) one, and maintaining multiple network connections will do much more cpu wakeups, wasting memory and that will cause battery drain.

So, using GCM/GMS is a must. Maybe fallback to self-made service will help you on the device without GCM.

Note about "GTalk client". Your application can not be full-featured GTalk client: Google Talk was a part of Google Platform (now rebranded as Hangouts). It uses many Google proprietary XMPP extensions, which you can't use from third-party client: you don't have access to message history/sync, groupchats, you can't register device in GCM for GTalk notifications, voice/video features are only partially accessible. And with GTalk deprecation in favour of Hangouts - you lost basic XMPP compatibility too and don't have modern API at all.

So, if you want to build messaging/voip application, you should have your own server, with own users database, authentication (your app still be able to authenticate users as Google users and import their contacts too), messaging storage and sync, and of course your messaging server should register users in GCM and forward messages to Google services when your client app is not connected.

There are some commercial platforms (like Parse, but I have not tested it) which gives you that "third-party server component" and SDK you should plug in to your client app and they will care about all "server-side" part of your project, including GCM.

0
Flow On