我的应用程序中有提醒。我在其中创建了每日重复的通知。现在我的通知中心可以看到多个通知。而且由于它是可操作的通知用户可以点击“是”或“否”。如何检测可执行通知的触发日期
我已经监听它如下:
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”
你希望它在iOS的7工作,但你使用仅适用于iOS 10的'UNUserNotificationCenterDelegate'。 –
请帮助我。我是iOS新手。我应该使用什么。 –
用户“UILocalNotification”或更改您的目标受众。 79%在ios10上https://developer.apple.com/support/app-store/ –