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.
No comments:
Post a Comment