I have a reminder in my application. In which I created a daily repeat notification. Now in my notification center you can see several notifications. And since this action can be triggered, the user can click Yes or No.
I listen to him as follows:
import Foundation
import UserNotifications
import UserNotificationsUI
class NotificationSetup: NSObject, UNUserNotificationCenterDelegate
{
let requestIdentifier = "SampleRequest"
let salahTimeArr =
[
[ "salahName": "Fajar", "time" : DateComponents.init( hour: 7, minute: 0 )],
[ "salahName": "Zuhar", "time" : DateComponents.init( hour: 14, minute: 0 )],
[ "salahName": "Asar", "time" : DateComponents.init( hour: 18, minute: 0 )],
[ "salahName": "Maghrib", "time" : DateComponents.init( hour: 19, minute: 30 )],
[ "salahName": "Isha", "time" : DateComponents.init( hour: 22, minute: 0 ) ]
]
func createNotificationCategory()
{
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound])
{
(granted, error) in
let actionYes = UNNotificationAction(identifier: "yes", title: "Yes", options: [])
let actionNo = UNNotificationAction(identifier: "no", title: "No", options: [])
let category = UNNotificationCategory(identifier: "SalahOfferedActionableNotification", actions: [actionYes, actionNo], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
}
}
func setNotifications()
{
self.createNotificationCategory()
if ( UserDefaults.standard.bool(forKey: "isNotificationSet") == false )
{
for salahDict in salahTimeArr
{
print(salahDict["salahName"])
let content = UNMutableNotificationContent()
content.title = "Did you offer"
content.subtitle = salahDict["salahName"] as! String
content.body = "Its Fard!"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "SalahOfferedActionableNotification"
let date = salahDict["time"] as! DateComponents
let trigger = UNCalendarNotificationTrigger.init(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
let notificationSingletonObj = UNUserNotificationCenter.current()
notificationSingletonObj.delegate = self
notificationSingletonObj.add(request)
{
(error) in
if (error != nil)
{
print(error?.localizedDescription)
}
}
}
UserDefaults.standard.set( true, forKey: "isNotificationSet" )
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
let salahName = response.notification.request.content.subtitle
let date = response.notification.date
if response.actionIdentifier == "yes"
{
print("User tapped 'Yes' button on notification for Salah: \(salahName)")
UserDefaults.standard.set( true, forKey: salahName )
Calculation().addSalah(forSalah : salahName, offered: 1 )
userDefaults.set( Date(), forKey: "dataCapturedForDate")
}
}
}
I want to read the notification date (launch date). I found the following
let date = response.notification.date
But, as the Apple documentation says, it supports iOS 10 and later. But I want support before iOS7.

, , .
, . , . - . - "A" "B"
