iOS Overview: How to Create Local Notifications in iOS10:
From iOS10 and onwards Apple deprecated UILocalNotification . We need to use UNNotificationRequest instead. UNNotificationRequest is def...
This blog is for giving an overview of what is recent in iOS technology and for other mobile related topics. I am here by sharing what I have learned and wish to use this Blog as an open discussion forum. I have just started blogging. Please give your suggestions as well, it will help me to improve myself.
Monday, 19 September 2016
How to Create Local Notifications in iOS10
From iOS10 and onwards Apple deprecated UILocalNotification. We need to use UNNotificationRequest instead.
UNNotificationRequest is defined under UserNotifications framework. UNNotificationRequest consists of the following two objects or properties.
(a) UNNotificationContent
It defines the contents on the notification. For local notifications, create a UNMutableNotificationContent object and configure the contents of that object instead.
(b) UNNotificationTrigger
This is for defining an event that triggers the delivery of the notification.
Before triggering a notification, the first step from the App we should do is requesting authorisation for user interactions. The first time your app requests authorization, the user is alerted and given an opportunity to deny or grant that authorization.
Once the notification is triggered and the application is in background , notification UI will pop up with a sound.
UNNotificationRequest is defined under UserNotifications framework. UNNotificationRequest consists of the following two objects or properties.
(a) UNNotificationContent
It defines the contents on the notification. For local notifications, create a UNMutableNotificationContent object and configure the contents of that object instead.
(b) UNNotificationTrigger
This is for defining an event that triggers the delivery of the notification.
Before triggering a notification, the first step from the App we should do is requesting authorisation for user interactions. The first time your app requests authorization, the user is alerted and given an opportunity to deny or grant that authorization.
Requesting authorization for user interactions in Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; | |
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) | |
completionHandler:^(BOOL granted, NSError * _Nullable error) { | |
// Enable or disable features based on authorization. | |
}]; |
Requesting authorization for user interactions in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let center = UNUserNotificationCenter.currentNotificationCenter() | |
center.requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in | |
// Enable or disable features based on authorization. | |
} |
Creating a simple local notification in Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; | |
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" | |
arguments:nil]; | |
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body" | |
arguments:nil]; | |
content.sound = [UNNotificationSound defaultSound]; | |
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 | |
repeats:NO]; | |
UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:kNotificationIdentifier | |
content:content | |
trigger:trigger]; | |
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; | |
[center addNotificationRequest:notificationRequest | |
withCompletionHandler:^(NSError * _Nullable error) { | |
NSLog(@"completed!"); | |
}]; |
Creating a simple local notification in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let content = UNMutableNotificationContent() | |
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil) | |
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil) | |
content.sound = UNNotificationSound.default() | |
// Deliver the notification in five seconds. | |
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false) | |
let request = UNNotificationRequest.init(identifier: "notification_sample_2", content: content, trigger: trigger) | |
// Schedule the notification. | |
let center = UNUserNotificationCenter.current() | |
center.add(request) |
Once the notification is triggered and the application is in background , notification UI will pop up with a sound.
Subscribe to:
Posts (Atom)