我的應用程序中有提醒。我在其中創建了每日重複的通知。現在我的通知中心可以看到多個通知。而且由於它是可操作的通知用戶可以點擊「是」或「否」。如何檢測可執行通知的觸發日期
我已經監聽它如下:
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/ –