Local Notifications with Background Fetch

This is how we used Background Fetch to show Local Notifications instead of traditional Push Notifications in GitHawk. We used FMDB to track delivered alerts in our implementation.

One of our values with GitHawk is to respect your privacy, especially when it comes to your GitHub data. We always connect directly to GitHub via the official API, and store all of your authentication information on your phone. No servers involved!

However, that poses a big challenge for adding our most-requested feature for GitHawk: Push Notifications.

Traditional Apple Push Notifications (APN) require you to:

Ask the user permission

Get a callback with the device token

Send that token to your server and save it

Whenever you need to send a notification, send content along with the token to Apple’s APN servers

That middle step (sending a token to our servers) wont work for GitHawk because we don’t want to send your data off of your phone.

GitHawk has been using app background fetch APIs (glorified polling) to update the badge icon for months. We decided to piggy-back off of this existing feature and “fake” push notifications.

Local Notifications

Sending local notifications is easy with the UserNotifications framework:

GitHub’s API only returns 50 notifications at a time, so limiting the history to 1,000 shouldn’t have any noticeable repeated notifications.

All that’s left to do is iterate through remaining map.values and fire off some UNNotificationRequests!

Wrapping Up

While not as fully featured as traditional Apple Push Notifications (realtime alerts, rich notifications, etc), using Local Notifications and Background Fetch got us an MVP of the most-requested feature in users hands quick. And most importantly, it avoids any need for us to store private authentication data off people’s phones.