Best practices for pubnub on android
Asked Answered
A

1

8

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?

Angelita answered 7/3, 2012 at 19:57 Comment(0)
X
10

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.
Xiphoid answered 7/3, 2012 at 20:59 Comment(7)
The links are broken. Do you know if those projects are still available?Contradict
I found it on github!: github.com/pubnub/pubnub-api/tree/…Remotion
The Service which wakes up every 5 minutes to ensure service stays alive, does this not drain the battery? Is this what happens with other chat apps like whatsapp?Patron
@AyrtonSenna the background service requires your mobile radio receiver to be active before it can receive TCP traffic. This means you must use level 4 wakelock on Android manifest. You must do the same for GCM. The PubNub Real-Time Network when compared to GCM requires up to 5% less batter drain which is amazing. The 5min wake you refer to is of no consequence as the WakeLock setting is more interesting to be aware of in this case.Xiphoid
@pubnub can you expound on, Connecting to Multiple PubNub Channels at the Same Time, a little more. Multiplexing using android is a real need. A little guidance from the pubnub team would help exponential. tyTook
@Took yes you are correct and multiplexing is automatic in our SDKs by connecting to multiple channels on a Single TCP Socket. Here is our JavaScript details: github.com/pubnub/… which is identical in functionality in our Android SDK: github.com/pubnub/java/tree/master/… where you'll subscribe to each channel with the subscribe() method and SDK will automatically update the TCP socket.Xiphoid
I tried github.com/pubnub/java/tree/master/… but still I'm not able to figure out how to use setResumeOnReconnect() .Serous

© 2022 - 2024 — McMap. All rights reserved.