내 앱에 알림이 있습니다. 매일 알림을 반복 작성했습니다. 이제 알림 센터에서 여러 알림을 볼 수 있습니다. 실행 가능한 알림이므로 "예"또는 "아니요"를 누릅니다.실행 가능한 알림의 실행 날짜를 감지하는 방법
나는 다음과 같이 그것을 위해 수신기를했습니다 :
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
을 발견하지만 사과 문서 말한다으로는 & 나중에 아이폰 OS 10을 지원합니다. 하지만 iOS7까지 지원이 필요합니다.
명확하게 이해하는 알림 센터 이미지를 참조하시기 바랍니다. 동일한 제목, 설명 & 모든 알림 정보가 비슷합니다. 사용자가 어떤 알림을 탭했는지 감지하려고합니다. 날씨는 월요일 통보 또는 화요일 통보였습니다. 날씨가 "A"또는 "B"입니다
앱 위임에서 로컬 통지를
을 해고하는 대신
UNUserNotificationCenterDelegate
의
UILocalNotification
를 사용해야하지만 당신은있어 iOS 10에서만 사용할 수있는 'UNUserNotificationCenterDelegate'를 사용하십시오. –제발 도와주세요. iOS에 익숙하지 않습니다. 무엇을 사용해야합니까? –
사용자 'UILocalNotification'또는 타겟 고객을 변경하십시오. 79 %가 ios10에 있습니다. https://developer.apple.com/support/app-store/ –