2

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)?

Community
  • 1
  • 1
kaqqao
  • 10,809
  • 4
  • 50
  • 101

3 Answers3

2

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

Amedeo Baragiola
  • 314
  • 4
  • 14
  • Why would one need a broadcast receiver to show notifications? – Flow Jan 04 '15 at 12:07
  • I don't really need it, It's just I want my service to show a notification when the app is closed and do some other stuff when it's opened, so I created two Broadcast Receivers with the same filter to achieve my goal – Amedeo Baragiola Jan 04 '15 at 13:26
  • 1
    How do you make the service always alive? From what I've been reading one should make a sticky service, but quite a few Android versions are bugged and don't restart them... How are you getting around that? – kaqqao Jan 04 '15 at 22:31
  • This is another question and I'm sure there is a more detailed explanation here on Stack OverFlow... Anyway I've made it sticky and I've overridden the method onTaskRemoved() adding some code to restart it. Thumbs up appreciated! – Amedeo Baragiola Jan 04 '15 at 23:00
1

You should use an Android Service that holds the XMPPConnection.

See also: - aSmack as a service - https://github.com/Flowdalic/asmack/wiki/Should-applications-using-aSmack-use-foreground-Services%3F

Community
  • 1
  • 1
Flow
  • 22,048
  • 13
  • 91
  • 147
0

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.

vitalyster
  • 3,774
  • 3
  • 17
  • 26
  • Everything I've read about GCM suggests it's to be used with a custom server-side application that sends notifications. Am I misinterpreting this? – kaqqao Jan 04 '15 at 11:20
  • Using GCM is not a must. – Flow Jan 04 '15 at 12:03
  • @Flow the only case when your can avoid using GCM is building your own android-based platform with self-made ecosystem, like Amazon and Nokia do. In most other cases your application will be the first candidate for user complains. – vitalyster Jan 04 '15 at 12:12
  • Is custom server a must with GCM? – kaqqao Jan 04 '15 at 12:18
  • @vitalyster I see your point, and I think you're right. But seeing my ambition isn't to build a WhatsApp killer but to just provide a way for my super simple game I'm making in order to learn Android development to send messages between users, building an entire infrastructure would be way over my head. – kaqqao Jan 04 '15 at 13:07