Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for pubnub on android

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?

like image 518
Quentin Avatar asked Mar 07 '12 19:03

Quentin


1 Answers

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:

  • Create a Service that is Started when when Android Boots: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/BootReceiver.java
  • UnSubscribe/Disconnect Example Code when you want to stop listening on a PubNub Channel: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/MainActivity.java - Listening to multiple channels is easy by placing the blocking pubnub.Subscribe() method inside a Thread.

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.
like image 159
Stephen Blum Avatar answered Nov 03 '22 00:11

Stephen Blum