私は自分のアプリにリマインダーを持っています。私は通知の毎日の繰り返しを作成しました。これで、通知センターに複数の通知が表示されるようになりました。行動可能な通知であるため、ユーザーは「はい」または「いいえ」をタップできます。実行可能な通知のトリガーされた日付を検出する方法
私は次のようにそれのためのリスナーをしました:
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
を見つけしかし、Appleのドキュメントが言うように、それは、&後でiOSの10をサポートしています。しかし、私はiOS7までのサポートが欲しい。
明確にそれを理解するために、通知センターの画像を参照してください。 私は同じタイトル、説明&すべての通知情報に似ています。そして、どのユーザーがどの通知をタップしたのかを検出したい。天気は月曜日の通知か火曜日の通知でした。天気、それは "A" または "B" である
ローカル通知を発射する
UNUserNotificationCenterDelegate
の代わりに
UILocalNotification
を使用する必要がiOS7をサポートします「UNUserNotificationCenterDelegate」を使用してください。これはiOS 10でのみ利用可能です。 –だから私を助けてください。 iOSの新機能です私は何を使うべきですか? –
ユーザー "UILocalNotification"またはターゲットオーディエンスを変更します。 79%がios10にありますhttps://developer.apple.com/support/app-store/ –