• No results found

ITP 342 Mobile App Development. Notifications

N/A
N/A
Protected

Academic year: 2021

Share "ITP 342 Mobile App Development. Notifications"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

ITP 342

Mobile App

Development

(2)

3 Types

•  Apple provides three different types of

notifications in iOS: NSNotificationCenter,

UILocalNotification (Local Notifications), and Remote (Push) Notifications.

•  The three notifications have three very

different uses.

•  NSNotificationCenter and Notification Center

(3)
(4)
(5)

Noti

fi

cation Center

•  Notification Center is a feature added in iOS 5

that allows users to control and manage local and remote notifications.

•  One part of Notification Center is the window

shade pulled down from the status bar showing active notifications.

•  The other part of Notification Center is accessed

from SettingsNotification Center.

•  It allows users to set the type of alert displayed

for notifications, change the sound played or even disable notifications completely.

(6)
(7)

Local and Push Noti

fi

cations

•  The essential purpose of both local and push

notifications is to enable an application to inform its users that it has something for them – for example, a message or an upcoming appointment – when the application isn’t running in the foreground.

•  The essential difference between local notifications

and push notifications is simple:

–  Local notifications are scheduled by an application and

delivered on the same device.

–  Push notifications, also known as remote notifications,

are sent by your server to the Apple Push Notification service, which pushes the notification to devices.

(8)

Same to Users

•  Local and push notifications appear the

same to the users

•  Users see notifications in the following ways:

–  Displaying an alert or banner –  Badging the app's icon

(9)

Same to Users

•  From a user’s perspective the meaning of

notifications is the same.

–  Both local and remote notifications indicate that there is something of interest in the app.

•  Users control how the device and specific

applications installed on the device should handle notifications.

–  They can also selectively enable or disable push notification types (that is, icon badging, alert

(10)

Di

erent to Apps

•  Local and push notifications appear different to apps

–  If your app is frontmost, the application:

didReceiveRemoteNotification: or

application: didReceiveLocalNotification:

method is called on its app delegate.

–  If your app is not frontmost or not running, you handle the

notifications by checking the options dictionary passed to

the application:

didFinishLaunchingWithOptions: of your app

delegate for either the

UIApplicationLaunchOptionsLocalNotificatio

nKey or

UIApplicationLaunchOptionsRemoteNotificati

(11)

Local Noti

fi

cations

•  A local notification is an instance of UILocalNotification

with three general kinds of properties:

–  Scheduled time: You must specify the date and time the

operating system delivers the notification; this is known as the

fire date. You may qualify the fire date with a specific time zone so that the system can make adjustments to the fire date when the user travels. You can also request the operating system to reschedule the notification at a regular interval (weekly,

monthly, and so on).

–  Notification type: This category includes the alert message,

the title of the action button, the application icon badge number, and a sound to play.

–  Custom data: Local notifications can include a dictionary of

(12)

UILocalNoti

fi

cation

•  https:// developer.apple.com/ library/ios/documentation/ iPhone/Reference/ UILocalNotification_Class/ Reference/Reference.html

(13)

UILocalNoti

fi

cations

•  A UILocalNotification is set within an iOS app to

trigger at a specific time.

–  During setup, you can specify that an “Open” button

be present on the alert presented to the user.

•  When the UILocalNotification is set up, iOS

adds an entry to Notification Center for the app.

–  This entry allows the user to change if a notification

message should be displayed, how the notification is displayed (alert message or notification pull down) and what sound plays.

(14)

UILocalNoti

fi

cations

•  When the trigger time is reached, several things can

happen, some of them in sequence:

–  If the app is not in the foreground, the notification is sent

to Notification Center.

–  If the user has enabled alerts for the app, the type of alert

selected will be shown. If the app set up the notification to have an “Open” button, the app is launched or brought to the foreground if the user touches the button.

–  If the app is not currently launched, it will be and the

method application:

didFinishLaunchingWithOptions: in the App

Delegate is called followed by a call to application:

WillEnterForeground: and finally followed by a call

to application:

(15)

UILocalNoti

fi

cations

•  When the trigger time is reached, several things

can happen, some of them in sequence:

–  If the app is launched but is not in the foreground, it

is brought to the foreground and the method

application: WillEnterForeground: in the App Delegate is called followed by a call to the

method application:

didReceiveLocalNotification:.

–  If the app is in the foreground, the method

application:

didReceiveLocalNotification: in the App Delegate is called bypassing any action from

(16)

UILocalNoti

fi

cations

•  Both methods application:

didFinishLaunchingWithOptions: and

application:

didReceiveLocalNotification: are passed the

notification while application:

WillEnterForeground: is not.

•  The method application:

didReceiveLocalNotification: should be

used to take any actions that are required every time a local notification is received.

•  The method application:

didFinishLaunchingWithOptions: can be used

to take actions specific to when the app is launched because of receiving a local notification.

(17)

Push Noti

fi

cations

•  A push notification is a short message that a

provider has delivered to the operating system of a device or computer; the operating system, in turn, informs the user of a client application that there is data to be downloaded, a message to be viewed, and so on.

•  If the user enables this feature (on iOS) and the

application is properly registered, the notification is delivered to the operating system and

possibly to the application.

•  Apple Push Notification service is the primary

(18)

Push Noti

fi

cations

•  Remote notifications, also called Apple Push

Notifications (APN), are used to tell an app that something outside the device has

occurred.

–  The app can then act on this information. •  This process requires the action of two

remote servers – the Apple Push Notification Server (APNS) and a server maintained by the app developer.

(19)

Remote Noti

fi

cations

•  https://developer.apple.com/library/ios/

documentation/NetworkingInternet/ Conceptual/RemoteNotificationsPG/ Chapters/ApplePushService.html

(20)

Push Noti

fi

cations

•  The operating system receives a push

notification on behalf of the application and alerts the user.

–  Once alerted, users may choose to launch the application, which then downloads the data from its provider.

–  If an application is running when a notification comes in, the application can choose to handle the notification directly.

(21)

Push Noti

fi

cations

•  As its name suggests, Apple Push Notification service

(APNs) uses a push design to deliver notifications to devices and computers.

•  A push design differs from its opposite, a pull design,

in that the recipient of the notification passively listens for updates rather than actively polling for them.

•  A push design makes possible a wide and timely

dissemination of information with few of the scalability problems inherent with pull designs.

•  APNs uses a persistent IP connection for

(22)

Push Noti

fi

cations

•  Most of a push notification consists of a

payload: a property list containing

APNs-defined properties specifying how the user is to be notified.

–  For performance reasons, the payload is deliberately small.

–  Although you may define custom properties for the payload, you should never use the remote-notification mechanism for data transport

because delivery of push notifications is not guaranteed.

(23)

Push Noti

fi

cations

•  APNs retains the last notification it receives

from a provider for an application on a

device; so, if a device comes online and has not received the notification, APNs pushes the stored notification to it.

•  A device running iOS receives push

notifications over both Wi-Fi and cellular connections.

(24)

Push Noti

fi

cations

•  In iOS, Wi-Fi is used for push notifications only if there is

no cellular connection or if the device is an iPod touch. •  For some devices to receive notifications via Wi-Fi, the

device’s display must be on (that is, it cannot be sleeping) or it must be plugged in.

•  The iPad, on the other hand, remains associated with the

Wi-Fi access point while asleep, thus permitting the delivery of push notifications.

•  The Wi-Fi radio wakes the host processor for any

incoming traffic.

•  Sending notifications too frequently negatively impacts

the device’s battery life – devices must access the network to receive notifications.

(25)

Noti

fi

cation Center vs.

NSNoti

fi

cationCenter

•  Notification Center manages notifications

generated by UILocalNotification and Remote Notifications only.

•  Notifications from NSNotificationCenter (within

an app) are not managed by Notification Center, making the naming confusing.

•  NSNotificationCenter dates back to Mac OSX

10.0 while Notification Center was added in iOS 5, so there are historical reasons for this

(26)

NSNoti

fi

cationCenter

•  https://developer.apple.com/library/mac/

documentation/Cocoa/Reference/Foundation/ Classes/nsnotificationcenter_Class/Reference/ Reference.html

(27)

NSNoti

fi

cationCenter

•  Since the very first release of MacOS X,

NSNotificationCenter has been used to send notifications within an app.

•  These notifications are invisible to users, but

allow one part of an app to let another part know something has happened and some action is

required.

•  A typical use for this is to notify a view that a

download of data from a remote server is

complete, and the data needs to be processed and the view updated to reflect it.

(28)

Noti

fi

cations

•  Notifications are an incredibly useful way to send

messages (and data) between objects that otherwise don't know about each other

–  Think of it like a radio station broadcasting messages: the

station (sender) broadcasts the message, and listeners can choose to listen in and receive some (or all) of the broadcast messages

•  For example, you might have a network reachability

check in your app delegate, and post notifications whenever the reachability changes

–  Other objects would listen for that notification and react

accordingly when the network goes up or down

•  Some Cocoa frameworks send notifications when

(29)

Noti

fi

cations

•  Suppose we have unsaved changes when

the user presses the home button

•  The model could perform one more save

before the app enters the background if only it knew

•  Let's explore how the model can ensure that

it is notified whenever the app is about to enter the background

(30)

Noti

fi

cations

•  In iOS 4.0 and up, the application sends

notifications when an app is about to move to the background and when it returns from the background

•  Your app's objects and controllers can listen

for these notifications and stop actions (or

save data) when the app is about to quit, and resume when it becomes active again

(31)

Limits of Delegation

•  Delegation is a one-to-one relationship

–  UIApplication has one delegate –  UITextField has one delegate

–  UITableView has one data source and one delegate

•  How can multiple parties learn about an

(32)

Noti

fi

cations

•  One-to-many broadcasts

•  Any interested party can subscribe

•  One-way communication

–  Source to listener

–  No feedback loop like delegation provides •  Any class can post notifications

–  You can make your own

•  Notifications are limited to the current

process

(33)

Application Noti

fi

cations

•  UIApplication also posts notifications for many of the same events

it messages the delegate on

–  UIApplicationDidBecomeActiveNotification –  UIApplicationDidChangeStatusBarFrameNotification –  UIApplicationDidChangeStatusBarOrientationNotification –  UIApplicationDidEnterBackgroundNotification –  UIApplicationDidFinishLaunchingNotification –  UIApplicationDidReceiveMemoryWarningNotification –  UIApplicationProtectedDataDidBecomeAvailable –  UIApplicationProtectedDataWillBecomeUnavailable –  UIApplicationSignificantTimeChangeNotification –  UIApplicationWillChangeStatusBarOrientationNotification –  UIApplicationWillChangeStatusBarFrameNotification –  UIApplicationWillEnterForegroundNotification –  UIApplicationWillResignActiveNotification –  UIApplicationWillTerminateNotification

(34)

UIApplication Events

UIApplication NSNotificationCenter UIApplicationDelegate AnswersViewController FortunesModel FortunesViewController applicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification

(35)

Observing Noti

fi

cations

•  Register with the Notification Center

–  Pass a valid selector

•  Must take a single NSNotification parameter

–  Make sure your selector has a colon

// USCFortunesModel.m!

!

- (id)init {!

self = [super init];!

if (self) {!

[[NSNotificationCenter defaultCenter] addObserver: self!

selector: @selector(appDidEnterBackground:)!

name: UIApplicationDidEnterBackgroundNotification!

object: [UIApplication sharedApplication]];!

}!

return self;!

(36)

Observing Noti

fi

cations

•  Implement your observer method

–  Remember the NSNotification parameter –  Call your save method

// USCFortunesModel.m!

!

- (void) appDidEnterBackground:(NSNotification *)notification {!

NSLog(@"Entering background: %@", notification);!

[self save];!

(37)

Removing Noti

fi

cations

•  You must eventually stop observing

–  Failing to do so may produce a crash later •  Stop when you have what your want

•  Or unsubscribe when your object goes away

–  The dealloc method is your last chance

// USCFortunesModel.m!

!

- (void) dealloc {!

[[NSNotificationCenter defaultCenter] removeObserver:self];!

(38)

Remove Observers

•  The programmer must remember to remove

the observer for notifications when they’re no longer needed.

•  This is often done in the dealloc method

for the class where the notification was registered.

•  The dealloc method exists and is called

even when ARC is used for memory management.

(39)

NSNoti

fi

cationCenter vs. Delegates

•  NSNotificationCenter can be used in a similar way to

delegates, notifying a different part of the app that some action is required.

•  Actions called via NSNotificationCenter can be changed

while a delegate method is always the same.

–  It’s also possible to have one trigger cause several different

actions.

•  As a general rule, if a single, non-changing action is

required for a specific trigger, a delegate method may be the best option.

–  If a trigger must sometimes result in one action and other times

have a different action, or if a trigger needs to result in multiple actions in different locations, NSNotificationCenter is a better choice.

(40)

Resources

•  https://developer.apple.com/library/ios/ documentation/NetworkingInternet/ Conceptual/RemoteNotificationsPG/ Chapters/WhatAreRemoteNotif.html#// apple_ref/doc/uid/TP40008194-CH102-SW1

References

Related documents

The Send Test Notification page uses information from your Apple Push Notification Service (APNS) or Google Cloud Messaging for Android (GCM) setup to configure a synchronous

•  Features –  Storage (data, DB) –  Push notifications –  Usage analytics –  Dashboard/UI –  Social integration –  User administration. –  Custom

EMC Documentum Mobile Application leverages Apple Push Notification service to intimate the mobile users when a task got assigned to that user or when favorite item properties

At the Apple Push Certificates Portal, you accept a license agreement and upload the intermediate certificate that you downloaded from the ZENworks Mobile Management

To install mobile security profiles on devices, your organization must request an Apple Push Notification (APN) certificate from Apple and then upload it to the Mobile

Installing Trend Micro Mobile Security for Enterprise requires Apple Push Notification service (APNs) certificate to manage iOS devices.. The process includes the following

Before you can manage iOS devices with your device management portal you will need an Apple Push Notification service (APNs) certificate.. The Apple Push Notification service

Mobile device management of computers and iOS devices (both institutionally owned and personally owned) requires communication with Apple Push Notification service (APNs) on the