Pacemaker


Source link: https://github.com/raveeshbhalla/Pacemaker

Pacemaker

GCM (or Google Cloud Messaging) is an amazing tool that you could use in a variety of ways within your Android app. It had its flaws, but when used correctly, it allowed developers to get a lot done with very little work, that too for free with no rate limits (albeit potential throttling if you went nuts).

Google chose to create GCM largely as a black box, where you were simply told if a push message has been successfully received by the server, and then passing that push message on to your app. You would have fairly little information regarding what happened in between, and for most developers that was fine. Debugging on a few devices would rarely show any errors, with pushes delivered instantaneously.

However, if you send a large enough volume of push messages, you might have noticed a few times that the numbers simply don't add up. Reading and re-reading your code would have told you that everything's fine, and that maybe users simply haven't had access to internet or have uninstalled the app itself. Turns out, that's not always the case. There's a pretty serious bug out there that could be dropping your delivery success rate significantly.

While the link above goes deeply into it, I'll summarize here: Play Services sends a heartbeat to its servers every 15 minutes on WiFi and 28 minutes on mobile networks to ensure constant connectivity, even when the user doesn't touch his device. This is done to prevent a TCP idle timeout from breaking the connection. Under most circumstances, this is ok. Unfortunately, a number of WiFi routers, OEMs, ISPs and mobile carriers actually perform a timeout much faster, which results in long periods when a push simply cannot be delivered to a user.

This issue was spotted earlier by a few developers who went on to develop apps that "fixed" push notifications for you. However, as a developer with Haptik, I couldn't rely on our users having this app installed. So, to solve this issue, I ended up adding the functionality into our app itself. The impact has been exactly what we needed: looking through the Developer Console data, we can see for ourselves that issues with GCM messages stuck in "accepted" state has dropped to a negligible amount (in fact, I haven't seen any stuck) with all of them making it through to our users devices.

Right from the day we built out Pacemaker into our app, our goal was to offer the benefits of the library to other developers. So go ahead, check through your GCM data to see if you are having delivery issues, and add this to your project if you want to fix it.

How to use

First, add the library to your project using Maven Central

repositories {

  maven {
 url "https://oss.sonatype.org/content/repositories/snapshots/" 
}
 
}
  dependencies {

  compile 'in.raveesh:pacemaker:0.1.0-SNAPSHOT' 
}
 

We've exposed two functions to start Pacemaker, one which sends linear heartbeats, and one which sends them after exponential gaps.

Linear
/**  * Starts a linear repeated alarm that sends a broadcast to Play Services, which in turn sends a heartbeat  * @param context Context from your application  * @param delay Gap between heartbeats in minutes  */ Pacemaker.scheduleLinear(Context context, int delay) 
Exponential
/**  * Starts an exponential alarm that sends a broadcast to Play Services, which in turn sends a heartbeat  * @param context Context from your application  * @param delay Time in which to send first broadcast. Subsequent broadcasts would be at exponential intervals  * @param max The max time till which the broadcasts should be sent. Once past this limit, no more heartbeats are sent  */ Pacemaker.scheduleExponential(Context context, int delay, int max)  

Which should I use

For apps, such as Haptik, where it can be reasonably guessed when a push notification is most likely to be received, the exponential system works well since it does not result in heartbeats being sent continuously. For example, at Haptik, it is far more likely to receive a push immediately after a user moves out of the app, and rarely after a significant portion of time.

For apps, such as social messaging applications, where a message might come at any time and it is important to notify the user immediately, the linear option would be the right one. Please be careful with regards to the time gap that you choose.

Impact on battery

In our testing (including real world data), we have seen no noticeable impact of the exponential option on battery life. We haven't used the linear system, but we believe if you need that, your only real alternative is to keep a service running constantly in the background, which would be worse.

Haptik Open Source Challenge

As we've said in the past, we at Haptik are highly grateful for the open source community for the role it has played in getting us where we are, and are always looking to contribute back with work such as Pacemaker. However, while we'll continue shipping code, we really would like others to get involved as well.

For this reason, we're launching the Haptik Open Source Challenge. Every once in a while, we'll pick up one of our libraries and invite the community to contribute to it. Contributors stand the chance of winning cash prizes and jobs at Haptik.

Today (September 16, 2015) we're starting the first Open Source Challenge, inviting contributions for Pacemaker. Your goal, if you do accept the challenge, is to build out a functionality that allows multiple apps on the device to use Pacemaker, without harming the user's battery life.

The challenge is open until 12 noon on 21st September, 2015. So go ahead, fork us, do a good deed, and once you're done, submit a pull request. If you have any questions, E-mail me (Raveesh Bhalla) or reach out to me on Twitter.

Apps using Pacemaker

Want to list your app here? Simply submit a pull request with an edit to the Readme

Resources

Image zoomer provides easy way to add zoom animations to you ImageButton. You can set the animation duration and reverse mode to reverse the animation.

Animation of liquid loading button.

The MarkedView is the markdown text viewer.

A simple easy to use library that lets you edit pictures on the fly with easy. Photo filter lets you apply a large number of filters all while maintaining maximum image quality.

A better DropDownMenu solution.

Realm database browser.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes