SlideShare a Scribd company logo
1 of 60
Download to read offline
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
Introduction and Review
Push Notifications
Darryl Bleau
APNs Engineering Manager
2
Agenda
Topics we’ll be covering
•Introduction to notifications
•Push architecture
•Local and push notifications on iOS
•Push notifications on Mac OS X
•Best practices
3
•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 Notifications
Questions and answers
4
Push Notifications
Overview
•Send notifications directly to target device
•Intended as a ‘tap on the shoulder’
•Should reflect server-side state
5
Push Notifications
Store 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
Push Notification Service Architecture
Your Server
Token
Payload
Token
Payload
7
Binary Protocols
Your Server PayloadToken
1057...
Identifier
20
Token
Length
dcaf30b...
Device
Token
13
Payload
Length
{“aps”:{“badge”:1}}
Payload (JSON)
0
Command
20
Token
Length
dcaf30b...
Device
Token
13
Payload
Length
{“aps”:{“badge”:1}}
Payload (JSON)
0
Cmd
127482...
Expiry
1
Cmd
8
Sending Notifications
Message 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
{
"aps" : {
"alert" : "Jen: Sushi at 10?",
"badge" : 1,
"sound" : "Jingle.aiff"
},
"acme1" : "conversation9964"
}
Whitespace-stripped JSON is a good idea
Sizing Considerations
96 bytes
150 bytes
{"aps":{"alert":"Jen: Sushi at 10?","badge":1,
"sound":"Jingle.aiff"},"acme1":"conversation9964"}
10
Over to the client side
Push Notifications
Your Server
11
Push and local notifications
Notifications on iOS
James Callender
iOS Software Engineer
12
Push Notifications
13
When to use them
•Social networking
•Current events
•Games
Using Push Notifications
14
Using Push Notifications
When not to use them
•Delivering critical application information
■ Push may not be available
■ Instead, pull data from server
15
Application launch
Registering 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
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
•Uniquely identifies the device
■ Separate from the UDID
The device token
Registering for Notifications
96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d082288aaebaf
•May change
■ Call registration API on every launch
■ Don’t depend on a cached copy
18
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
Creating Push Notifications
Badges
•Specified using a positive integer
{
"aps" : {
"badge" : 1
}
}
•Omitting will clear the badge
{
"aps" : { }
}
{
"aps" : {
"badge" : 1
}
}
{
"aps" : { }
}
20
Creating Push Notifications
Sounds
•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
Alerts
•Simplest form is just a string value
Creating Push Notifications
{
"aps" : {
"alert" : "Jen: Sushi at 10?"
}
}
22
Creating Push Notifications
Customizing 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
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
Local Notifications
25
How they are similar
Local Notifications vs. Push Notifications
Appearance: badges, alerts, and sounds
iOS acts on behalf of your app
26
Push Notifications vs. Local Notifications
How they are different
Originate from server
Single shot
Originate from your app
Scheduled, repeatable
Push Notifications Local Notifications
27
When to use them
•Alarm Clock
•Reminder
•Location
Using Local Notifications
28
Using Local Notifications
When not to use them
•Alerts and errors
■ UIAlertView
•Calendar events
■ EventKit if possible
29
Creating Local Notifications
UILocalNotification Overview
•Badge
NSInteger applicationIconBadgeNumber
•Alerts
NSString *alertBody
BOOL hasAction
NSString *alertAction
NSString *alertLaunchImage
•Sound
NSString *soundName
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
•Metadata
NSDictionary *userInfo
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
•Metadata
NSDictionary *userInfo
30
Creating Local Notifications
Badges
•Use the applicationIconBadgeNumber property
UILocalNotification *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
Alerts
•Set the alertBody property
UILocalNotification *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
Localizing your alerts
•Setting the localized key
UILocalNotification *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
Alert Launch Images
•alertLaunchImage will change your
app’s launch image
Creating Local Notifications
34
Alert Launch Images
•alertLaunchImage will change your
app’s launch image
note.alertLaunchImage = @”Default-watch.png”;
Creating Local Notifications
35
Creating Local Notifications
Sounds
• Use the soundName property
UILocalNotification *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
•Badge
NSInteger applicationIconBadgeNumber
•Alerts
NSString *alertBody
BOOL hasAction
NSString *alertAction
NSString *alertLaunchImage
•Sound
NSString *soundName
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
•Metadata
NSDictionary *userInfo
Creating Local Notifications
UILocalNotification Overview
•Badge
NSInteger applicationIconBadgeNumber
•Alerts
NSString *alertBody
BOOL hasAction
NSString *alertAction
NSString *alertLaunchImage
•Sound
NSString *soundName
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
37
A Word About Dates
Universal 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
Creating Local Notifications
Scheduling
•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
•Use repeatInterval and repeatCalendar
note.repeatInterval =
note.repeatCalendar = [NSCalendar currentCalendar];
NSWeekCalendarUnit;NSDayCalendarUnit;
Creating Local Notifications
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Scheduling repeating notifications
40
•Badge
NSInteger applicationIconBadgeNumber
•Alerts
NSString *alertBody
BOOL hasAction
NSString *alertAction
NSString *alertLaunchImage
•Sound
NSString *soundName
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
•Metadata
NSDictionary *userInfo
Creating Local Notifications
UILocalNotification Overview
•Scheduling
NSDate *fireDate
NSTimeZone *timeZone
•Repeating
NSCalendarUnit repeatInterval
NSCalendar *repeatCalendar
•Metadata
NSDictionary *userInfo
41
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
Local Notification Delivery
Getting 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
Push Notifications on Mac OS X
Jason Thorpe
Mac OS X Software Engineering Manager
44
Push Notifications on Mac OS X
How 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
Push Notifications on Mac OS X
How 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
Push Notifications on Mac OS X
Server-side impact
•Different Push provider certificates for iOS and Mac versions of
an application
■ Clients must identify themselves!
47
Push Notifications on Mac OS X
Server-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
Push Notifications on Mac OS X
Developer considerations
•Applications require a provisioning profile
from the Apple Developer Certificate Utility
49
Push Notifications on Mac OS X
Developer considerations
•Only applications distributed through the
Mac App Store may use Push notifications
50
Registering for Notifications
Application 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
Registering for Notifications
Delegate 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
•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
Demo
Darrin Jewell
Mac OS X Software Engineer
54
Best Practices
Push Notifications
Darryl Bleau
APNs Engineering Manager
55
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
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
Bill Dudney
Application Frameworks Evangelist
Bill Dudney <dudney@apple.com
Apple Developer Forums
http://devforums.apple.com
Sample Application: PushyMac
http://developer.apple.com/library/mac/#samplecode/PushyMac/Introduction/Intro.html
More Information
58
Labs
Local and Push Notifications Lab Internet and Web Lab A
Friday 9:00–11:15AM
59
60

More Related Content

Similar to Confidential sessions—push and local notifications

How to build a server and a iPhone client application using the Apple Push No...
How to build a server and a iPhone client application using the Apple Push No...How to build a server and a iPhone client application using the Apple Push No...
How to build a server and a iPhone client application using the Apple Push No...Shu Masuda
 
Configure &amp; send push notification on i os device
Configure &amp; send push notification on i os deviceConfigure &amp; send push notification on i os device
Configure &amp; send push notification on i os deviceShepHertz
 
Push Notification with Unity in iOS using App42 Backend
Push Notification with Unity in iOS using App42 BackendPush Notification with Unity in iOS using App42 Backend
Push Notification with Unity in iOS using App42 BackendShepHertz
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceJen Looper
 
REST is not enough: Using Push Notifications to better support your mobile cl...
REST is not enough: Using Push Notifications to better support your mobile cl...REST is not enough: Using Push Notifications to better support your mobile cl...
REST is not enough: Using Push Notifications to better support your mobile cl...Juan Gomez
 
Automation testing on ios platform using appium
Automation testing on ios platform using appiumAutomation testing on ios platform using appium
Automation testing on ios platform using appiumAmbreen Khan
 
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28Frédéric Harper
 
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...Amazon Web Services
 
Background Fetch - the most powerful API you've never heard of
Background Fetch - the most powerful API you've never heard ofBackground Fetch - the most powerful API you've never heard of
Background Fetch - the most powerful API you've never heard ofmoliver816
 
System insight without Interference
System insight without InterferenceSystem insight without Interference
System insight without InterferenceTony Tam
 
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015WWDC 15 - Apple's Developer Event @ 8-12 June, 2015
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015Burcu Geneci
 
Intro to Continuous Integration at SoundCloud
Intro to Continuous Integration at SoundCloudIntro to Continuous Integration at SoundCloud
Intro to Continuous Integration at SoundCloudgarriguv
 
Introduction to Titanium and how to connect with a PHP backend
Introduction to Titanium and how to connect with a PHP backendIntroduction to Titanium and how to connect with a PHP backend
Introduction to Titanium and how to connect with a PHP backendJoseluis Laso
 
Android studio
Android studioAndroid studio
Android studioAndri Yabu
 
Xamarin.iOS introduction
Xamarin.iOS introductionXamarin.iOS introduction
Xamarin.iOS introductionGuido Magrin
 
SensorStudio deep dive (IDC 2016)
SensorStudio deep dive (IDC 2016)SensorStudio deep dive (IDC 2016)
SensorStudio deep dive (IDC 2016)Herve Blanc
 

Similar to Confidential sessions—push and local notifications (20)

How to build a server and a iPhone client application using the Apple Push No...
How to build a server and a iPhone client application using the Apple Push No...How to build a server and a iPhone client application using the Apple Push No...
How to build a server and a iPhone client application using the Apple Push No...
 
Configure &amp; send push notification on i os device
Configure &amp; send push notification on i os deviceConfigure &amp; send push notification on i os device
Configure &amp; send push notification on i os device
 
Push Notification with Unity in iOS using App42 Backend
Push Notification with Unity in iOS using App42 BackendPush Notification with Unity in iOS using App42 Backend
Push Notification with Unity in iOS using App42 Backend
 
Telerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT ConferenceTelerik AppBuilder Presentation for TelerikNEXT Conference
Telerik AppBuilder Presentation for TelerikNEXT Conference
 
REST is not enough: Using Push Notifications to better support your mobile cl...
REST is not enough: Using Push Notifications to better support your mobile cl...REST is not enough: Using Push Notifications to better support your mobile cl...
REST is not enough: Using Push Notifications to better support your mobile cl...
 
Automation testing on ios platform using appium
Automation testing on ios platform using appiumAutomation testing on ios platform using appium
Automation testing on ios platform using appium
 
push_notification
push_notificationpush_notification
push_notification
 
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28
Firefox OS, fixing the mobile web - FITC Toronto - 2014-04-28
 
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...
(MBL301) Beyond the App - Extend Your User Experience with Mobile Push Notifi...
 
Background Fetch - the most powerful API you've never heard of
Background Fetch - the most powerful API you've never heard ofBackground Fetch - the most powerful API you've never heard of
Background Fetch - the most powerful API you've never heard of
 
Azure notification hubs
Azure notification hubsAzure notification hubs
Azure notification hubs
 
Complete iOS Toolkit
Complete iOS ToolkitComplete iOS Toolkit
Complete iOS Toolkit
 
System insight without Interference
System insight without InterferenceSystem insight without Interference
System insight without Interference
 
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015WWDC 15 - Apple's Developer Event @ 8-12 June, 2015
WWDC 15 - Apple's Developer Event @ 8-12 June, 2015
 
Intro to Continuous Integration at SoundCloud
Intro to Continuous Integration at SoundCloudIntro to Continuous Integration at SoundCloud
Intro to Continuous Integration at SoundCloud
 
Introduction to Titanium and how to connect with a PHP backend
Introduction to Titanium and how to connect with a PHP backendIntroduction to Titanium and how to connect with a PHP backend
Introduction to Titanium and how to connect with a PHP backend
 
Android studio
Android studioAndroid studio
Android studio
 
Getting Started with AWS IoT
Getting Started with AWS IoTGetting Started with AWS IoT
Getting Started with AWS IoT
 
Xamarin.iOS introduction
Xamarin.iOS introductionXamarin.iOS introduction
Xamarin.iOS introduction
 
SensorStudio deep dive (IDC 2016)
SensorStudio deep dive (IDC 2016)SensorStudio deep dive (IDC 2016)
SensorStudio deep dive (IDC 2016)
 

More from Jigar Maheshwari

More from Jigar Maheshwari (9)

Poster iphones
Poster iphonesPoster iphones
Poster iphones
 
Hyperloop
HyperloopHyperloop
Hyperloop
 
Xcode 6 release_notes
Xcode 6 release_notesXcode 6 release_notes
Xcode 6 release_notes
 
Introducing collection views - Mark Pospesel
Introducing collection views - Mark PospeselIntroducing collection views - Mark Pospesel
Introducing collection views - Mark Pospesel
 
Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)
 
Autolayout
AutolayoutAutolayout
Autolayout
 
iOS HIG
iOS HIGiOS HIG
iOS HIG
 
UIKit User Interface Catalog ios7
UIKit  User Interface Catalog ios7UIKit  User Interface Catalog ios7
UIKit User Interface Catalog ios7
 
iOS 7 Transition guide
iOS 7 Transition guideiOS 7 Transition guide
iOS 7 Transition guide
 

Recently uploaded

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRnishacall1
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceanilsa9823
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7Pooja Nehwal
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceanilsa9823
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Pooja Nehwal
 

Recently uploaded (7)

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 

Confidential sessions—push and local notifications

  • 1. 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
  • 2. Introduction and Review Push Notifications Darryl Bleau APNs Engineering Manager 2
  • 3. Agenda Topics we’ll be covering •Introduction to notifications •Push architecture •Local and push notifications on iOS •Push notifications on Mac OS X •Best practices 3
  • 4. •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 Notifications Questions and answers 4
  • 5. Push Notifications Overview •Send notifications directly to target device •Intended as a ‘tap on the shoulder’ •Should reflect server-side state 5
  • 6. Push Notifications Store 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
  • 7. Push Notification Service Architecture Your Server Token Payload Token Payload 7
  • 8. Binary Protocols Your Server PayloadToken 1057... Identifier 20 Token Length dcaf30b... Device Token 13 Payload Length {“aps”:{“badge”:1}} Payload (JSON) 0 Command 20 Token Length dcaf30b... Device Token 13 Payload Length {“aps”:{“badge”:1}} Payload (JSON) 0 Cmd 127482... Expiry 1 Cmd 8
  • 9. Sending Notifications Message 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
  • 10. { "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964" } Whitespace-stripped JSON is a good idea Sizing Considerations 96 bytes 150 bytes {"aps":{"alert":"Jen: Sushi at 10?","badge":1, "sound":"Jingle.aiff"},"acme1":"conversation9964"} 10
  • 11. Over to the client side Push Notifications Your Server 11
  • 12. Push and local notifications Notifications on iOS James Callender iOS Software Engineer 12
  • 14. When to use them •Social networking •Current events •Games Using Push Notifications 14
  • 15. Using Push Notifications When not to use them •Delivering critical application information ■ Push may not be available ■ Instead, pull data from server 15
  • 16. Application launch Registering 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
  • 17. 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
  • 18. •Uniquely identifies the device ■ Separate from the UDID The device token Registering for Notifications 96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d082288aaebaf •May change ■ Call registration API on every launch ■ Don’t depend on a cached copy 18
  • 19. 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
  • 20. Creating Push Notifications Badges •Specified using a positive integer { "aps" : { "badge" : 1 } } •Omitting will clear the badge { "aps" : { } } { "aps" : { "badge" : 1 } } { "aps" : { } } 20
  • 21. Creating Push Notifications Sounds •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
  • 22. Alerts •Simplest form is just a string value Creating Push Notifications { "aps" : { "alert" : "Jen: Sushi at 10?" } } 22
  • 23. Creating Push Notifications Customizing 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
  • 24. 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
  • 26. How they are similar Local Notifications vs. Push Notifications Appearance: badges, alerts, and sounds iOS acts on behalf of your app 26
  • 27. Push Notifications vs. Local Notifications How they are different Originate from server Single shot Originate from your app Scheduled, repeatable Push Notifications Local Notifications 27
  • 28. When to use them •Alarm Clock •Reminder •Location Using Local Notifications 28
  • 29. Using Local Notifications When not to use them •Alerts and errors ■ UIAlertView •Calendar events ■ EventKit if possible 29
  • 30. Creating Local Notifications UILocalNotification Overview •Badge NSInteger applicationIconBadgeNumber •Alerts NSString *alertBody BOOL hasAction NSString *alertAction NSString *alertLaunchImage •Sound NSString *soundName •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar •Metadata NSDictionary *userInfo •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar •Metadata NSDictionary *userInfo 30
  • 31. Creating Local Notifications Badges •Use the applicationIconBadgeNumber property UILocalNotification *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
  • 32. Alerts •Set the alertBody property UILocalNotification *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
  • 33. Localizing your alerts •Setting the localized key UILocalNotification *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
  • 34. Alert Launch Images •alertLaunchImage will change your app’s launch image Creating Local Notifications 34
  • 35. Alert Launch Images •alertLaunchImage will change your app’s launch image note.alertLaunchImage = @”Default-watch.png”; Creating Local Notifications 35
  • 36. Creating Local Notifications Sounds • Use the soundName property UILocalNotification *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
  • 37. •Badge NSInteger applicationIconBadgeNumber •Alerts NSString *alertBody BOOL hasAction NSString *alertAction NSString *alertLaunchImage •Sound NSString *soundName •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar •Metadata NSDictionary *userInfo Creating Local Notifications UILocalNotification Overview •Badge NSInteger applicationIconBadgeNumber •Alerts NSString *alertBody BOOL hasAction NSString *alertAction NSString *alertLaunchImage •Sound NSString *soundName •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar 37
  • 38. A Word About Dates Universal 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
  • 39. Creating Local Notifications Scheduling •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
  • 40. •Use repeatInterval and repeatCalendar note.repeatInterval = note.repeatCalendar = [NSCalendar currentCalendar]; NSWeekCalendarUnit;NSDayCalendarUnit; Creating Local Notifications Monday Tuesday Wednesday Thursday Friday Saturday Sunday Scheduling repeating notifications 40
  • 41. •Badge NSInteger applicationIconBadgeNumber •Alerts NSString *alertBody BOOL hasAction NSString *alertAction NSString *alertLaunchImage •Sound NSString *soundName •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar •Metadata NSDictionary *userInfo Creating Local Notifications UILocalNotification Overview •Scheduling NSDate *fireDate NSTimeZone *timeZone •Repeating NSCalendarUnit repeatInterval NSCalendar *repeatCalendar •Metadata NSDictionary *userInfo 41
  • 42. 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
  • 43. Local Notification Delivery Getting 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
  • 44. Push Notifications on Mac OS X Jason Thorpe Mac OS X Software Engineering Manager 44
  • 45. Push Notifications on Mac OS X How 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
  • 46. Push Notifications on Mac OS X How 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
  • 47. Push Notifications on Mac OS X Server-side impact •Different Push provider certificates for iOS and Mac versions of an application ■ Clients must identify themselves! 47
  • 48. Push Notifications on Mac OS X Server-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
  • 49. Push Notifications on Mac OS X Developer considerations •Applications require a provisioning profile from the Apple Developer Certificate Utility 49
  • 50. Push Notifications on Mac OS X Developer considerations •Only applications distributed through the Mac App Store may use Push notifications 50
  • 51. Registering for Notifications Application 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
  • 52. Registering for Notifications Delegate 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
  • 53. •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
  • 54. Demo Darrin Jewell Mac OS X Software Engineer 54
  • 55. Best Practices Push Notifications Darryl Bleau APNs Engineering Manager 55
  • 56. 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
  • 57. 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
  • 58. Bill Dudney Application Frameworks Evangelist Bill Dudney <dudney@apple.com Apple Developer Forums http://devforums.apple.com Sample Application: PushyMac http://developer.apple.com/library/mac/#samplecode/PushyMac/Introduction/Intro.html More Information 58
  • 59. Labs Local and Push Notifications Lab Internet and Web Lab A Friday 9:00–11:15AM 59
  • 60. 60