get step-by-step instructions on implementing notifications in your apps

60
These are confidential sessions—please refrain from streaming, blogging, or taking pictures On iOS and Mac OS X Session 517 Using Local and Push Notifications Darryl Bleau APNs Engineering Manager 1

Upload: jigar-maheshwari

Post on 10-May-2015

218 views

Category:

Mobile


2 download

DESCRIPTION

Get step-by-step instructions on implementing notifications in your apps.

TRANSCRIPT

Page 1: Get step-by-step instructions on implementing notifications in your apps

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

On iOS and Mac OS X

Session 517

Using Local and Push Notifications

Darryl BleauAPNs Engineering Manager

1

Page 2: Get step-by-step instructions on implementing notifications in your apps

Introduction and ReviewPush Notifications

Darryl BleauAPNs Engineering Manager

2

Page 3: Get step-by-step instructions on implementing notifications in your apps

AgendaTopics we’ll be covering

• Introduction to notifications• Push architecture• Local and push notifications on iOS• Push notifications on Mac OS X• Best practices

3

Page 4: Get step-by-step instructions on implementing notifications in your apps

• What is a notification?• User visible information reflecting some event

• Why should I use notifications?• Ensure time-sensitive delivery even when your app isn’t running

• How does push compare to poll?• Pushes are server-driven and immediate, polls are app-driven and latent

Introduction to NotificationsQuestions and answers

4

Page 5: Get step-by-step instructions on implementing notifications in your apps

Push NotificationsOverview

• Send notifications directly to target device• Intended as a ‘tap on the shoulder’• Should reflect server-side state

5

Page 6: Get step-by-step instructions on implementing notifications in your apps

Push NotificationsStore and forward

• Notifications sent to offline devices are stored for future delivery

• Reconnecting devices receive the latest notification per app

• Notifications can be sent with an expiry time■ Expiry only affects storage; notifications to online devices are delivered regardless of expiry time

6

Page 7: Get step-by-step instructions on implementing notifications in your apps

Push Notification Service Architecture

Your Server

Token

Payload

Token

Payload

7

Page 8: Get step-by-step instructions on implementing notifications in your apps

Binary Protocols

Your Server PayloadToken

1057...

Identifier

20

TokenLength

dcaf30b...

DeviceToken

13

PayloadLength

{“aps”:{“badge”:1}}

Payload (JSON)

0

Command

20

TokenLength

dcaf30b...

DeviceToken

13

PayloadLength

{“aps”:{“badge”:1}}

Payload (JSON)

0

Cmd

127482...

Expiry

1

Cmd

8

Page 9: Get step-by-step instructions on implementing notifications in your apps

Sending NotificationsMessage payload

• Strict RFC 4627 JSON• Human readable• Compact• Easily mapped to NSDictionary • 256 byte maximum• Keys outside “aps” are available for your own use

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

9

Page 10: Get step-by-step instructions on implementing notifications in your apps

{

"aps" : {

"alert" : "Jen: Sushi at 10?",

"badge" : 1,

"sound" : "Jingle.aiff"

},

"acme1" : "conversation9964"

}

Whitespace-stripped JSON is a good ideaSizing Considerations

96 bytes

150 bytes

{"aps":{"alert":"Jen: Sushi at 10?","badge":1,

"sound":"Jingle.aiff"},"acme1":"conversation9964"}

10

Page 11: Get step-by-step instructions on implementing notifications in your apps

Over to the client sidePush Notifications

Your Server

11

Page 12: Get step-by-step instructions on implementing notifications in your apps

Push and local notificationsNotifications on iOS

James CallenderiOS Software Engineer

12

Page 13: Get step-by-step instructions on implementing notifications in your apps

Push Notifications

13

Page 14: Get step-by-step instructions on implementing notifications in your apps

When to use them

• Social networking• Current events• Games

Using Push Notifications

14

Page 15: Get step-by-step instructions on implementing notifications in your apps

Using Push NotificationsWhen not to use them

• Delivering critical application information■ Push may not be available■ Instead, pull data from server

15

Page 16: Get step-by-step instructions on implementing notifications in your apps

Application launchRegistering for Notifications

-(void)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)options {

UIRemoteNotificationType myTypes = UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeBadge;

[application registerForRemoteNotificationTypes:myTypes];

}

• UIApplication to register■ Pass the types you want to receive

16

Page 17: Get step-by-step instructions on implementing notifications in your apps

Delegate callbacks

• Successful registration- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token

■ Phone home with your token

• Failure to register- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

■ Check your provisioning profile for entitlements■ Push not supported in the simulator

Registering for Notifications

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token

■ Phone home with your token

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

■ Check your provisioning profile for entitlements■ Push not supported in the simulator

17

Page 18: Get step-by-step instructions on implementing notifications in your apps

• Uniquely identifies the device■ Separate from the UDID

The device tokenRegistering for Notifications

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d082288aaebaf

• May change■ Call registration API on every launch■ Don’t depend on a cached copy

18

Page 19: Get step-by-step instructions on implementing notifications in your apps

Message payload

• aps dictionary reserved for the sound, badge, or alert keys■ All keys optional

{

"aps" : {

"alert" : "Jen: Sushi at 10?",

"badge" : 1,

"sound" : "Jingle.aiff"

},

"acme1" : "conversation9964"

}

• Rest of payload is for your app

Creating Push Notifications

19

Page 20: Get step-by-step instructions on implementing notifications in your apps

Creating Push NotificationsBadges

• Specified using a positive integer{ "aps" : { "badge" : 1 }}

• Omitting will clear the badge{ "aps" : { }}

{ "aps" : { "badge" : 1 }}

{ "aps" : { }}

20

Page 21: Get step-by-step instructions on implementing notifications in your apps

Creating Push NotificationsSounds

• Filename in application bundle{ "aps" : { "sound" : "Jingle.aiff" }}

• Or “default” for the default notification noise{ "aps" : { "sound" : "default" }}

• Vibration is automatic

{ "aps" : { "sound" : "Jingle.aiff" }}

21

Page 22: Get step-by-step instructions on implementing notifications in your apps

Alerts

• Simplest form is just a string value

Creating Push Notifications

{ "aps" : { "alert" : "Jen: Sushi at 10?" }}

22

Page 23: Get step-by-step instructions on implementing notifications in your apps

Creating Push NotificationsCustomizing alerts

• Replace the alert string with a dictionary{ "aps" : { "alert" : { "loc-key" : "MSG_POSTED", "loc-args" : [ "Jen", "iPhone" ], “action-loc-key” : “VIEW” } }}Spanish.lproj/Localizable.strings“MSG_POSTED” = “%@ acaba de fijar un mensaje para usted sobre %@”;“VIEW” = “Ver”;

23

Page 24: Get step-by-step instructions on implementing notifications in your apps

Getting the payload

• If your app is running, you’ll only get- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

• If not, tapping View will launch your app with:- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSDictionary *userInfo = [launchOptions objectForKey:

UIApplicationLaunchOptionsRemoteNotificationKey];

...

}

Push Notification Delivery

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSDictionary *userInfo = [launchOptions objectForKey:

UIApplicationLaunchOptionsRemoteNotificationKey];

...

}

24

Page 25: Get step-by-step instructions on implementing notifications in your apps

Local Notifications

25

Page 26: Get step-by-step instructions on implementing notifications in your apps

How they are similarLocal Notifications vs. Push Notifications

Appearance: badges, alerts, and sounds

iOS acts on behalf of your app

26

Page 27: Get step-by-step instructions on implementing notifications in your apps

Push Notifications vs. Local NotificationsHow they are different

Originate from server

Single shot

Originate from your app

Scheduled, repeatable

Push Notifications Local Notifications

27

Page 28: Get step-by-step instructions on implementing notifications in your apps

When to use them

• Alarm Clock• Reminder• Location

Using Local Notifications

28

Page 29: Get step-by-step instructions on implementing notifications in your apps

Using Local NotificationsWhen not to use them

•Alerts and errors■ UIAlertView

•Calendar events■ EventKit if possible

29

Page 30: Get step-by-step instructions on implementing notifications in your apps

Creating Local NotificationsUILocalNotification Overview

• BadgeNSInteger applicationIconBadgeNumber

• AlertsNSString *alertBody

BOOL hasAction

NSString *alertAction

NSString *alertLaunchImage

• SoundNSString *soundName

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

• MetadataNSDictionary *userInfo

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

• MetadataNSDictionary *userInfo

30

Page 31: Get step-by-step instructions on implementing notifications in your apps

Creating Local NotificationsBadges

• Use the applicationIconBadgeNumber propertyUILocalNotification *note =

! [[UILocalNotification alloc] init];

note.applicationIconBadgeNumber = 3;

• Setting to zero will not clear the badge• Instead use UIApplication:

UIApplication *application =

![UIApplication sharedApplication];

application.applicationIconBadgeNumber = 0;

UILocalNotification *note =

! [[UILocalNotification alloc] init];

note.applicationIconBadgeNumber = 3;

31

Page 32: Get step-by-step instructions on implementing notifications in your apps

Alerts

• Set the alertBody propertyUILocalNotification *note =

! [[UILocalNotification alloc] init];

note.alertBody = @”Baseball game starting now”;

• Optionally set the alertAction property

Creating Local Notifications

UILocalNotification *note =

! [[UILocalNotification alloc] init];

note.alertBody = @”Baseball game starting now”;

note.hasAction = YES;

note.alertAction = @”Watch”;

32

Page 33: Get step-by-step instructions on implementing notifications in your apps

Localizing your alerts

• Setting the localized keyUILocalNotification *note =

[[UILocalNotification alloc] init];

note.alertBody = @”MSG_POSTED”;

note.hasAction = YES;

note.alertAction = @”SHOW_KEY”;

French.lproj/Localizable.strings

“MSG_POSTED” = “Le match de baseball commence dans 5 minutes”;

“SHOW_KEY” = “Regarder”;

Creating Local Notifications

UILocalNotification *note =

[[UILocalNotification alloc] init];

note.alertBody = @”MSG_POSTED”;

note.hasAction = YES;

note.alertAction = @”SHOW_KEY”;

33

Page 34: Get step-by-step instructions on implementing notifications in your apps

Alert Launch Images

• alertLaunchImage will change your app’s launch image

Creating Local Notifications

34

Page 35: Get step-by-step instructions on implementing notifications in your apps

Alert Launch Images

• alertLaunchImage will change your app’s launch imagenote.alertLaunchImage = @”Default-watch.png”;

Creating Local Notifications

35

Page 36: Get step-by-step instructions on implementing notifications in your apps

Creating Local NotificationsSounds

• Use the soundName propertyUILocalNotification *note =

! [[UILocalNotification alloc] init];

note.soundName = @”Squawk.aiff”;

• UILocalNotificationDefaultSoundName plays a default sound

UILocalNotification *note =

! [[UILocalNotification alloc] init];

note.soundName = @”Squawk.aiff”;

UILocalNotification *note =

! [[UILocalNotification alloc] init];

note.soundName = UILocalNotificationDefaultSoundName;

36

Page 37: Get step-by-step instructions on implementing notifications in your apps

• BadgeNSInteger applicationIconBadgeNumber

• AlertsNSString *alertBody

BOOL hasAction

NSString *alertAction

NSString *alertLaunchImage

• SoundNSString *soundName

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

• MetadataNSDictionary *userInfo

Creating Local NotificationsUILocalNotification Overview

• BadgeNSInteger applicationIconBadgeNumber

• AlertsNSString *alertBody

BOOL hasAction

NSString *alertAction

NSString *alertLaunchImage

• SoundNSString *soundName

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

37

Page 38: Get step-by-step instructions on implementing notifications in your apps

A Word About DatesUniversal Time vs. Wall Time

• “Universal” time■ Lonely NSDate■ Conference Call■ Stock Market Close

• “Wall” time

■ Lonely NSDate■ Conference call■ Stock market close

■ NSDate + NSTimeZone■ 9:00AM alarm■ Television show (Thursdays at 8:00PM EST)

38

Page 39: Get step-by-step instructions on implementing notifications in your apps

Creating Local NotificationsScheduling

• Use the fireDate and timeZone properties

UILocalNotification *note =

! [[UILocalNotification alloc] init];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *dateComps =

! [[NSDateComponents alloc] init];

[dateComps setDay:10];

[dateComps setMonth:6];

[dateComps setYear:2010];

[dateComps setHour:14];

note.fireDate = [calendar dateFromComponents:dateComps];

note.timeZone = [calendar timeZone];

UILocalNotification *note =

! [[UILocalNotification alloc] init];

note.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24];

note.timeZone = [[NSCalendar currentCalendar] timeZone];

39

Page 40: Get step-by-step instructions on implementing notifications in your apps

• Use repeatInterval and repeatCalendarnote.repeatInterval =

note.repeatCalendar = [NSCalendar currentCalendar];

NSWeekCalendarUnit;NSDayCalendarUnit;

Creating Local Notifications

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

Scheduling repeating notifications

40

Page 41: Get step-by-step instructions on implementing notifications in your apps

• BadgeNSInteger applicationIconBadgeNumber

• AlertsNSString *alertBody

BOOL hasAction

NSString *alertAction

NSString *alertLaunchImage

• SoundNSString *soundName

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

• MetadataNSDictionary *userInfo

Creating Local NotificationsUILocalNotification Overview

• SchedulingNSDate *fireDate

NSTimeZone *timeZone

• RepeatingNSCalendarUnit repeatInterval

NSCalendar *repeatCalendar

• MetadataNSDictionary *userInfo

41

Page 42: Get step-by-step instructions on implementing notifications in your apps

Scheduling

• Schedule and canceling with UIApplication- (void)scheduleLocalNotification:

(UILocalNotification *)notification;

- (void)cancelLocalNotification:

(UILocalNotification *)notification;

• Scheduling for background apps

Creating Local Notifications

- (void)scheduleLocalNotification:

(UILocalNotification *)notification;

- (void)cancelLocalNotification:

(UILocalNotification *)notification;

- (void)presentLocalNotificationNow:

(UILocalNotification *)notification;

42

Page 43: Get step-by-step instructions on implementing notifications in your apps

Local Notification DeliveryGetting the notification

• If your app is running, you’ll only get:- (void)application:(UIApplication *)application

didReceiveLocalNotification:(UILocalNotification *)notification

• If not, tapping View will launch your app with:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (void)application:(UIApplication *)application

didReceiveLocalNotification:(UILocalNotification *)notification

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

UILocalNotification *note = [launchOptions objectForKey:

UIApplicationLaunchOptionsLocalNotificationKey];

...

}

43

Page 44: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS X

Jason ThorpeMac OS X Software Engineering Manager

44

Page 45: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XHow they are the same

• AppKit API is the same as UIKit API• Notifications are delivered to running applications via your NSApplication delegate

• Icons are badged in Dock and LaunchPad for non-running applications

45

Page 46: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XHow they are different

• Badges only; no alerts or sounds• Application cannot be launched in response to a Push notification• No systemwide preferences for Push Notifications• No iOS-style local notifications

46

Page 47: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XServer-side impact

• Different Push provider certificates for iOS and Mac versions of an application■ Clients must identify themselves!

47

Page 48: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XServer-side impact

• Same Push payload can be sent to iOS and Mac applications■ You only have to construct it once!

{

"aps" : {

"alert" : "Jen: Sushi at 10?",

"badge" : 1,

"sound" : "Jingle.aiff"

},

"acme1" : "conversation9964"

}

48

Page 49: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XDeveloper considerations

• Applications require a provisioning profile from the Apple Developer Certificate Utility

49

Page 50: Get step-by-step instructions on implementing notifications in your apps

Push Notifications on Mac OS XDeveloper considerations

• Only applications distributed through the Mac App Store may use Push notifications

50

Page 51: Get step-by-step instructions on implementing notifications in your apps

Registering for NotificationsApplication launch

• NSApplication to register■ Pass the types you want to receive

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {

NSApplication *application = [aNotification object];

NSRemoteNotificationType myTypes = NSRemoteNotificationTypeBadges;

[application registerForRemoteNotificationTypes:myTypes];

}

51

Page 52: Get step-by-step instructions on implementing notifications in your apps

Registering for NotificationsDelegate callbacks

• Successful registration

• Failure to register- (void)application:(NSApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

■ Check your provisioning profile for entitlements

- (void)application:(NSApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token

■ Phone home with your token■ Make sure to distinguish between iOS and Mac versions of your app!

52

Page 53: Get step-by-step instructions on implementing notifications in your apps

• Payload is contained in userInfo{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

Push Notification Delivery

• Implement this in your NSApplication delegate- (void)application:(NSApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

Getting the payload

53

Page 54: Get step-by-step instructions on implementing notifications in your apps

Demo

Darrin JewellMac OS X Software Engineer

54

Page 55: Get step-by-step instructions on implementing notifications in your apps

Best PracticesPush Notifications

Darryl BleauAPNs Engineering Manager

55

Page 56: Get step-by-step instructions on implementing notifications in your apps

Best Practices

• Pull data while your app is running■ Push service may be turned off■ Apps should be fully functional without Push

• Upload the device token to your server often■ Users can sync and restore your application data without your server’s knowledge

• Don’t annoy your users with alerts■ Send notifications your user cares about■ If you don’t need them, don’t use them

56

Page 57: Get step-by-step instructions on implementing notifications in your apps

We do need them

• Primary notification type• For actionable information• Keep a server-side count• Test your logic

■ Count can change while inactive■ Notifications can arrive while active■ Ensure accurate information

Badges

57

Page 58: Get step-by-step instructions on implementing notifications in your apps

Bill DudneyApplication Frameworks EvangelistBill Dudney <[email protected]

Apple Developer Forumshttp://devforums.apple.com

Sample Application: PushyMachttp://developer.apple.com/library/mac/#samplecode/PushyMac/Introduction/Intro.html

More Information

58

Page 59: Get step-by-step instructions on implementing notifications in your apps

Labs

Local and Push Notifications Lab Internet and Web Lab AFriday 9:00–11:15AM

59

Page 60: Get step-by-step instructions on implementing notifications in your apps

60