Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS Push Notifications - Zero To Engagement

iOS Push Notifications - Zero To Engagement

To your users, push notifications seem completely obvious; something happened and this app is letting me know. In the background, there are a lot of things going on to make those notifications a reality. There are persistent socket connections to manage, binary protocols to navigate, and reliability issues to keep in mind. We'll take a look at the basics of what is needed to send push notifications from scratch, some of the pitfalls of doing it yourself, as well as mentioning some third party services that can help you along the way.

Adam Duke

July 11, 2013
Tweet

Other Decks in Programming

Transcript

  1. IOS PUSH NOTIFICATIONS ZERO TO ENGAGEMENT Created by Adam Duke

       @adamvduke @adamvduke http://adamvduke.com
  2. ABOUT ME Adam Duke Engineer/Co-Founder at Symmetric Infinity http://symmetricinfinity.com Working

    with iOS for about 4 years Working with Ruby/Rails for about 3 years I ❤ open source
  3. AGENDA What is a push notification? How do they work?

    Communicating with APNS Prerequisites for communicating Code Walkthrough Pitfalls Feedback Upcoming Changes/Features Third-party Providers
  4. HOW DO THEY WORK? 1. Apple maintains a persistent connection

    to each device 2. Something happens that you want to tell a user about 3. Package up some data in Apple's proprietary protocol 4. Send the data to Apple 5. Apple forwards the information to your user's device
  5. PROPRIETARY PROTOCOL? Uses a plain TCP socket secured by SSL

    A single notification looks like... Ewww Ain't nobody got time for that Most platforms have a library for this
  6. PREREQUISITES TO SEND A NOTIFICATION 1. Certificates to secure your

    socket These are tied to a specific bundle id e.g. com.apple.itunes.iphone Configure/Download from developer.apple.com 2. A device token Who do you want to send a notification to? We'll talk about how to get this in a minute. 3. A JSON hash of the data you'd like to send The "alert" key will be the text shown The "badge" key will set the badge on the icon The "sound" key identifies the sound to play https://zeropush.com/documentation/generating_certificates
  7. WHAT DO THEY LOOK LIKE? DEVICE TOKEN 3 9 9

    a a c 7 0 e 1 9 1 9 4 3 1 5 3 a 5 c 9 c 2 9 1 f d 7 5 f d a 3 6 9 c 3 c 6 d 5 c 7 c 3 e b 7 7 a 8 c 2 a e 6 0 1 b d 6 c 4
  8. WHAT DO THEY LOOK LIKE? NOTIFICATION PAYLOAD { " a

    p s " : { " a l e r t " : " Y o u ' v e g o t m a i l ! " , " b a d g e " : 9 , " s o u n d " : " s p e c i a l _ e d _ m a i l . a i f f " } } { " a p s " : { " a l e r t " : " A d a m D . s e n t y o u a m e s s a g e ! " , " b a d g e " : 9 , " s o u n d " : " b i n g b o n g . a i f f " } , " i n f o " : { " t y p e " : " c h a t " , " u s e r _ i d " : " 1 2 3 4 5 6 " } }
  9. HOW DO I GET A DEVICE TOKEN? Write some code!

    - ( v o i d ) a p p l i c a t i o n D i d B e c o m e A c t i v e : ( U I A p p l i c a t i o n * ) a p p l i c a t i o n { / * E v e r y t i m e t h e a p p b e c o m e s a c t i v e , a t t e m p t t o r e g i s t e r * / [ a p p l i c a t i o n r e g i s t e r F o r R e m o t e N o t i f i c a t i o n T y p e s : ( U I R e m o t e N o t i f i c a t i o n T y p e B a d g e | U I R e m o t e N o t i f i c a t i o n T y p e S o u n d | U I R e m o t e N o t i f i c a t i o n T y p e A l e r t ) ] ; }
  10. HOW DO I GET A DEVICE TOKEN? Remember this is

    Objective-C, so... Write a little more code! - ( v o i d ) a p p l i c a t i o n : ( U I A p p l i c a t i o n * ) a p p l i c a t i o n d i d R e g i s t e r F o r R e m o t e N o t i f i c a t i o n s W i t h D e v i c e T o k e n : ( N S D a t a * ) d e v i c e T o k e n { / * T u r n t h e d a t a i n t o a s t r i n g . S o m e o n e w i l l t e l l m e I c a n d o t h i s w i t h a r e g e x * / N S S t r i n g * t o k e n = [ d e v i c e T o k e n d e s c r i p t i o n ] ; t o k e n = [ t o k e n s t r i n g B y R e p l a c i n g O c c u r r e n c e s O f S t r i n g : @ " < " w i t h S t r i n g : @ " " ] ; t o k e n = [ t o k e n s t r i n g B y R e p l a c i n g O c c u r r e n c e s O f S t r i n g : @ " > " w i t h S t r i n g : @ " " ] ; t o k e n = [ t o k e n s t r i n g B y R e p l a c i n g O c c u r r e n c e s O f S t r i n g : @ " " w i t h S t r i n g : @ " " ] ; / * T O D O : S a v e t h e s t r i n g a l o n g w i t h a u s e r i d t o t h e s e r v e r s o w e c a n k n o w w h i c h d e v i c e s b e l o n g t o w h o m . * / }
  11. PITFALLS Apple expects you to maintain a persistent connection to

    the APN service Rapid connect/disconnect behavior may be seen as a DOS attack and get you black listed The communication over the connection is asynchronous If there is a problem with a notification Apple will reply, and close the connection Any notifications sent between the error and the reply must be sent again Connections that are idle for too long are closed with no warning
  12. FEEDBACK SERVICE 1. User deletes your app 2. You attempt

    to notify them 3. Apple realizes the app is no longer on their device 4. Apple marks that device as inactive 5. Query the feedback service 6. You mark the device inactive 7. Don't send notifications to inactive devices 8. If the device registers for notifications again, mark the device as active 9. Resume sending notifications
  13. THIRD-PARTY PROVIDERS 1. - A developer focused push notification provider

    Full disclosure, my business partner and I wrote this We're looking for beta testers!! 2. - A marketing focused push notification provider 3. - Cloud database and push notification provider Owned by Facebook, possibly subject to NSA Prism data collection 4. ZeroPush.com UrbanAirship.com Parse.com Push.IO