Mobile Push Notifications

57
Mobile: Push for Sync & Notifications By Mike Willbanks Software Engineering Manager CaringBridge

description

Mobile: for push and sync. Goes through implementing push notifications with PHP on the various 4 major players: Android (C2DM), Apple (APNS), Windows Phone (MPNS), BlackBerry (maybe works?).

Transcript of Mobile Push Notifications

Page 1: Mobile Push Notifications

Mobile: Push for

Sync & Notifications

By Mike Willbanks

Software Engineering Manager

CaringBridge

Page 2: Mobile Push Notifications

2

•Software Engineering Manager at CaringBridge

•Open Source Contributor

•Organizer of MNPHP

•Where you can find me:

Twitter: mwillbanks

G+: Mike Willbanks

IRC (freenode): lubs

Blog: http://blog.digitalstruct.com

About Mike…

Page 3: Mobile Push Notifications

3

•Overview of Push Notifications

•Android Push Notifications (C2DM)

•Apple Push Notifications (APNS)

•Microsoft Push Notifications

•BlackBerry Push Notifications

•Questions

Although you can bring them up at anytime!

Agenda

Page 4: Mobile Push Notifications

Overview What are they?

What is the benefit?

High level; how do these things work?

Page 5: Mobile Push Notifications

5

•Push Notifications…

Are a message pushed to a central location and delivered to you.

Are (often) the same thing at a pub/sub model.

In the Mobile Space…

• These messages often contain other technologies such as alerts, tiles,

or raw data.

What Are They

Page 6: Mobile Push Notifications

6

In Pictures…

Page 7: Mobile Push Notifications

Benefits of Push Notifications The benefits of push notifications are numerous; the

question is if you have an app and you are running services

to poll; why would you do such a thing!

Page 8: Mobile Push Notifications

8

One word… Battery Life

Page 9: Mobile Push Notifications

9

Impact of Polling

Page 10: Mobile Push Notifications

10

•Push notification services for mobile are highly efficient; it

runs in the device background and enables your application

to receive the message.

•The other part of this; if you implemented it otherwise you

would be polling. This not only wastes precious battery but

also wastes their bandwidth.

NOTE: This is not always true; if you are sending data to the phone

more often than a poll would do in 15 minutes; you are better off

implementing polling.

Battery Life

Page 11: Mobile Push Notifications

11

Can We Deliver?

Page 12: Mobile Push Notifications

12

•When you poll; things are generally 15+ minutes out to save

on battery. In a push notification these happen almost

instantly.

We’ve generally seen within 1-3s between sending a push

notification to seeing it arrive on the device.

•Additionally; push notifications can be sent to the device

even if it is offline or turned off.

•However, not all messages are guaranteed for delivery

You may hit quotas

Some notification servers only allow a single message to be in

queue at 1 time (some group by collapse key), and others remove

duplicates.

Delivery

Page 13: Mobile Push Notifications

How These Things Work The 10,000 foot view.

Page 14: Mobile Push Notifications

14

10,000 Foot View of C2DM

Page 15: Mobile Push Notifications

15

10,000 Foot View of APNS

Page 16: Mobile Push Notifications

16

10,000 Foot View of Windows Push

Page 17: Mobile Push Notifications

17

10,000 Foot View of who?

Page 18: Mobile Push Notifications

18

Oh, that’s right Blackberry!

Page 19: Mobile Push Notifications

Walking Through Android Understanding C2DM

Anatomy of a Message

Pushing Messages

Displaying Items on the Client

Page 20: Mobile Push Notifications

20

• It allows third-party application servers to send lightweight

messages to their Android applications.

•C2DM makes no guarantees about delivery or the order of

messages.

•An application on an Android device doesn’t need to be

running to receive messages.

• It does not provide any built-in user interface or other

handling for message data.

• It requires devices running Android 2.2 or higher that also have

the Market application installed.

• It uses an existing connection for Google services (Through the

Google Market)

Understanding C2DM

Page 21: Mobile Push Notifications

21

•First things first – you must sign up to actually utilize C2DM

http://code.google.com/android/c2dm/signup.html

C2DM only works on Android w/ Google Market; if you use get a

new Amazon Kindle Fire it would not work there.

•We will be utilizing a library based on ZF

https://github.com/mwillbanks/Zend_Service_Google_C2dm

Registering for C2DM

Page 22: Mobile Push Notifications

22

Anatomy of the Mobile App

Page 23: Mobile Push Notifications

23

•We must update the Manifest file to state additional

permissions.

•We will then create a broadcast receiver that will handle

the messages and registration.

How the Application Works

Page 24: Mobile Push Notifications

24

Example Manifest

Page 25: Mobile Push Notifications

25

Handling the Registration (or Unregistering)

•Registration / Registration Updates and Unregistering.

•Registration is generally on app start up.

•Be nice and allow your users to unregister from the push

notification service

Page 26: Mobile Push Notifications

26

Example Receiver

More at: http://bit.ly/bxOoMO towards end of article.

Page 27: Mobile Push Notifications

27

•Enough Java already; it is a PHP conference!

• Implementing the actual server is quick and pain free…

At least in my opinion (I did build this PHP implementation of

C2DM)

•Some limitations

200K messages per day by default; use them wisely however you

may request more.

1K message payload maximum.

You must implement incremental back off.

Implementing a Server

Page 28: Mobile Push Notifications

28

How the Server Works

Page 29: Mobile Push Notifications

29

•Example is on GitHub:

https://github.com/mwillbanks/Zend_Service_Google_C2dm

Code was a little too large to attempt to fit it into a slide

Using Zend_Service_Google_Gdata

Page 30: Mobile Push Notifications

Apple Push Notifications A brief walk-through on implementing notifications on the

iPhone.

Page 31: Mobile Push Notifications

31

• The maximum size allowed for a notification payload is 256

bytes.

• It allows third-party application servers to send lightweight

messages to their iPhone/iPad applications.

•Apple makes no guarantees about delivery or the order of

messages.

•An application on an iPhone/iPad device doesn’t need to be

running to receive messages.

•Message adheres to strict JSON but is abstracted away for us in

how we will be using it today.

•Messages should be sent in batches.

•A feedback service must be listened to.

Understanding APNS

Page 32: Mobile Push Notifications

32

•You must create a SSL certificate and key from the

provisioning portal

•After this is completed the provisioning profile will need to

be utilized for the application.

•Lastly, you will need to install the certificate and key on the

server.

In this case; you will be making a pem certificate.

Preparing to Implement Apple Push Notifications

Page 33: Mobile Push Notifications

33

Anatomy of the Application

Page 34: Mobile Push Notifications

34

•Registration

The application calls the registerForRemoteNotificationTypes:

method.

The delegate implements the

application:didRegisterForRemoteNotificationsWithDeviceToken:

method to receive the device token.

It passes the device token to its provider as a non-object, binary

value.

•Notification

By default this just works based on the payload; for syncing you

would implement this on the launch.

How the Application Works

Page 35: Mobile Push Notifications

35

Example of Handling Registration

Page 36: Mobile Push Notifications

36

Example of Handling Remote Notification

Page 37: Mobile Push Notifications

37

•Phew, through the Objective C.

•We will be leveraging APNS-PHP for this example as it

encompasses everything you will need to be able to send

notifications.

•http://code.google.com/p/apns-php/

Implementing the Server

Page 38: Mobile Push Notifications

38

How the Server Works

Page 39: Mobile Push Notifications

39

Example APNS-PHP Server

Page 40: Mobile Push Notifications

40

Example APNS-PHP Feedback Loop

Page 41: Mobile Push Notifications

Microsoft Push Notifications Well, I am not certain if they will find the market share yet

but hey; some people need to build apps for it!

Page 42: Mobile Push Notifications

42

• It allows third-party application servers to send lightweight

messages to their Windows Mobile applications.

•Microsoft makes no guarantees about delivery or the order of

messages. (See a pattern yet?)

•3 types of messages: Tile, Toast or Raw

•Limitations:

One push channel per app, 30 push channels per device, additional

adherence in order to send messages

3K Payload, 1K Header

•http://msdn.microsoft.com/en-us/library/ff402537.aspx

Understanding MPNS

Page 43: Mobile Push Notifications

43

•Upload a TLS certificate to Windows Marketplace

The Key-Usage value of the TLS certificate must be set to include

client authentication.

The Root Certificate Authority (CA) of the certificate must be one

of the CAs listed at: SSL Root Certificates for Windows Phone.

Stays authenticated for 4 months.

Set Service Name to the Common Name (CN) found in the

certificate's Subject value.

Install the TLS certificate on your web service and enable HTTP

client authentication.

•We will be using the following PHP class for this:

http://phpwindowsphonepush.codeplex.com/

Certainly not the most beautiful code but it works.

Preparing to Implement MPNS

Page 44: Mobile Push Notifications

44

Anatomy of MPNS

Page 45: Mobile Push Notifications

45

Registering for Push

Page 46: Mobile Push Notifications

46

Implementing the Callbacks for Notifications

Page 47: Mobile Push Notifications

47

•Real easy, real fast and a simple class…

I’m not too fond on the code to repeat myself but it would be easy

to clean up

http://phpwindowsphonepush.codeplex.com/

•Pretty much nothing else left to do but show you the

options!

Implementing the Server

Page 48: Mobile Push Notifications

48

Example Implementing MPNS w/ PHP

Page 49: Mobile Push Notifications

BlackBerry Push Notifications Are these even going to be needed in another year?

Page 50: Mobile Push Notifications

50

• It allows third-party application servers to send lightweight

messages to their BlackBerry applications.

•Allows a whopping 8K or the payload

•Uses WAP PAP 2.2 as the protocol

•Mileage may vary…

Only a sample php push server has ever been published.

Looks to be mainly for the Browser: http://bit.ly/fYl6rr

Understanding BlackBerry Push

Page 51: Mobile Push Notifications

51

Anatomy of BB Push

Page 52: Mobile Push Notifications

52

•They have a “Sample” but it is deep within their Push SDK.

Many of which are pre-compiled.

Documentation is hard to follow and the sample isn’t exactly

straight forward:

• Install the SDK then go to BPSS/pushsdk-low-level/sample-push-

enabled-app/ and unzip sample-push-enabled-app-1.1.0.16-sources.jar

• You will see several areas to get the push notifications going… Let’s

take a look.

Application Code

Page 53: Mobile Push Notifications

53

•You need to register with BlackBerry and have all of the

application details ready to go:

https://www.blackberry.com/profile/?eventId=8121

•Download the PHP library:

Updated to be OO; non-tested and a bit sloppy:

https://github.com/mwillbanks/BlackBerryPush

Original source: http://bit.ly/nfbHXp

Preparing to Implement

Page 54: Mobile Push Notifications

54

•Be aware… this code is highly alpha – never been tested.

•Let me know how it goes… Will be refactoring it and testing

it in the future.

Implementing BB Push w/ PHP

Page 55: Mobile Push Notifications

55

•ZF 2

I am looking as time permits to contribute implementations of the

4 big players push notification implementations to Zend

Framework. I am not sure when I will find the time but at some

point…

•BlackBerry

Who knows where they will end up. All I know is that developing

for it seems painful and the documentation is certainly not what I

would like to see.

There is a large need for a quality implementation but at the same

point developers are not highly interested in their platform.

Moving Forward

Page 56: Mobile Push Notifications

56

• Main Sites

Apple Push Notifications:

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Con

ceptual/RemoteNotificationsPG/Introduction/Introduction.html

Google C2DM (Android): http://code.google.com/android/c2dm/

Microsoft Push Notifications: http://msdn.microsoft.com/en-

us/library/ff402558(v=vs.92).aspx

BlackBerry Push Notifications:

http://us.blackberry.com/developers/platform/pushapi.jsp

• Push Clients:

Apns-php: http://code.google.com/p/apns-php/

ZF C2DM: https://github.com/mwillbanks/Zend_Service_Google_C2dm

MS: http://phpwindowsphonepush.codeplex.com/

BlackBerry: https://github.com/mwillbanks/BlackBerryPush

• Might be broken but at least better than what I found anywhere else

Resources

Page 57: Mobile Push Notifications

Questions? Give me feedback: http://joind.in/3766

Slides will be posted at joind.in later this afternoon.