Best practices for pubnub on android

前端 未结 1 902
梦如初夏
梦如初夏 2021-02-01 10:20

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 libra

相关标签:
1条回答
  • 2021-02-01 11:09

    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.
    0 讨论(0)
提交回复
热议问题