我在我的应用中有提醒。其中我每天都会重复通知。现在可以在我的通知中心看到多个通知。由于它的可操作通知用户可以点击"是"或"否"。
我聆听它如下:
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")
}
}
}
我想阅读通知日期(触发日期)。我找到了以下
let date = response.notification.date
但正如苹果文档所说它支持iOS 10&后来。但是我想要支持iOS7。
请参阅通知中心图像以清楚地理解。 我有相同的标题,描述&所有通知信息类似。我想检测用户点击的通知。天气是星期一的通知或星期二的通知。天气是" A"或" B"
答案 0 :(得分:0)
fireDate
属性UILocalNotification
。你可以使用它。另外,要支持iOS7,您需要使用UILocalNotification
代替UNUserNotificationCenterDelegate
发起本地通知
let localNotification = UILocalNotification()
let fireDate = Date()
localNotification.fireDate = fireDate
localNotification.alertBody = "Your alert message"
localNotification.repeatInterval = .day
UIApplication.shared.scheduleLocalNotification(localNotification)
在您的app delegate
中func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
notification.fireDate
}