Notification Center

When I first heard NSNotificationCenter, I thought “push notifications”. However, NSNotificationCenterhas nothing to do with sending or receiving push notifications in iOS. Rather, it is a communication tool internal to your app. It provides a way for one instance of a class/struct to notify one or more other class/struct instances about something. The goal in doing this is to enable those 1+ other class or struct instances to take appropriate action based the communication they receive. An common analogy comes to mind: think “radio tower”.NSNotificationCenter is the central hub that acts as a broadcaster of notifications.

Swift 3.0

// Define identifier

let notificationName = Notification.Name("NotificationIdentifier")

// Register to receive notification

NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification), name: notificationName, object: nil)

// Post notification

NotificationCenter.default.post(name: notificationName, object: nil)

// Stop listening notification

NotificationCenter.default.removeObserver(self, name: notificationName, object: nil);


UNNotification

Local notifications are primarily intended for apps with timer-based / region-based behaviours. UNNotificationRequest (UserNotifications.framework) build for same purpose.

History

  • iOS 3.0 : Remote local notification / Push notification service APNS
  • iOS 4.0 : Local notification
  • iOS 10.0 : UILocalNotification is deprecated. Replacing with UNNotificationRequest

Note: An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.

Setup

// Swift
import UserNotifications

// Objective-C (with modules enabled)
@import UserNotifications;

You manage notifications through a shared UNUserNotificationCenter object:

// Swift
let center = UNUserNotificationCenter.current()

// Objective-C
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

Authorization

As with the older notifications framework you need to have the user’s permission for the types of notification your App will use. Make the request early in your App life cycle such as in application:didFinishLaunchingWithOptions:. The first time your App requests authorization the system shows the user an alert, after that they can manage the permissions from settings. To create such request, add key ' ' in info.plist

There are four notification types, badge, sound, alert, carPlay you can combine as required. For example if you want both alerts and sound, here is how to authosize :

// Swift

let options: UNAuthorizationOptions = [.alert, .sound];

// Objective-C

UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

You make the actual authorization request using the shared notification center:

// Swift

center.requestAuthorization(options: options) {

(granted, error) in

if !granted {

print("Something went wrong")

}

}

// Objective-C

[center requestAuthorizationWithOptions:options

completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (!granted) {

NSLog(@"Something went wrong");

}

}];

The framework calls the completion handler with a boolean indicating if the access was granted and an error object which will be nil if no error occurred.

Note: The user can change the notifications settings for your App at any time. You can check the allowed settings with getNotificationSettings. This calls a completion block asynchronously with a UNNotificationSettings object you can use to check the authorization status or the individual notification settings:

// Swift

center.getNotificationSettings { (settings) in

if settings.authorizationStatus != .authorized {

// Notifications not allowed

}

}

// Objective-C

[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {

// Notifications not allowed

}

}];

Full Reference Here


APNS Push Notification

Important notes

results matching ""

    No results matching ""