I developed local Notifications
in iOS 10
. It is working perfectly. But now how should i code local notifications
and push notification
if user is using iOS 9
and above versions. Can anyone help please?
Below is code in iOS 10
import UIKit
import UserNotifications
@available(iOS 10.0, *)
class ViewController: UIViewController,UNUserNotificationCenterDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 10.0, *) {
//Seeking permission of the user to display app notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in })
UNUserNotificationCenter.current().delegate = self
}
}
//To display notifications when app is running inforeground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(_ sender: UIButton) {
if #available(iOS 10.0, *) {
//Setting content of the notification
let content = UNMutableNotificationContent()
content.title = "hello"
content.body = "notification pooped out"
content.badge = 1
//Setting time for notification trigger
let date = Date(timeIntervalSinceNow: 10)
var dateCompenents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)
//Adding Request
let request = UNNotificationRequest(identifier: "timerdone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}