Question

I'm using pubnub as a publish/subscribe channel between an android app and a server. Currently I'm thinking of how I will implement this.

I'm using the provided library for android (https://github.com/pubnub/pubnub-api/tree/master/android) but I think there will be some problems with the application lifecycle if I use it like it is now. (Correct me if i'm wrong)

I was thinking of implementing it as a service

What I want

  • The service has to keep on running until an hour (negotiable) after the last app usage. That's because we want to have notifications when a message comes in, but the app is not the currently used app.

  • How do i stop the service after one hour of non-activity of the app? Probably Android will kill it, but I want some control.

  • The Service must be able to trigger the app to change it's interface when specific messages come in (I was thinking of sending intents from the service when we receive a pubnub message?), pubnub will send data to the service, so I need a way to pass this data to the application (probably save it in a Bundle in the intent?)

  • I need to listen to multiple pubnub channels (max 2 at the same time), I think I will have to do this in multiple instances of this service?

I think I will do it like this:

  • Create a service that's started when the app starts

  • Let the service listen to a pubnub channel

  • When a message comes in, send an intent and use the intent filters

  • implement broadcasthandlers to listen to these internal intents

Is this the right way to do this? any hints?

Was it helpful?

Solution

You have an excellent set of questions an detailed points that I will talk about in this answer. You are using Android and you are interested in the conventions and best practices for PubNub Publish/Subscribe scenarios.

Your use case is very common and the best ways to build apps always vary dependent on application needs. However you definitely have the right idea and have asked all the right questions. You just needed some sample code and a direction to get started on implementing the specifics of your application needs. To define your needs in a list:

  • Connect/Disconnect Ability.
  • Always-on Background Service that can Send/Receive data and notify other apps via Android Intents.
  • Connecting to Multiple PubNub Channels at the Same Time.

So to get started I will provide you direct links to some examples and methods:

Regarding your thoughts - This IS the right way to do it:

  • Create a service that's started when the app starts
  • Let the Service listen to a PubNub Channel.
  • When a message comes in, send an intent and use the intent filters.
  • Implement BroadcastHandlers to listen to these internal intents.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top