首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测可操作通知的触发日期

如何检测可操作通知的触发日期
EN

Stack Overflow用户
提问于 2017-04-05 19:19:06
回答 1查看 554关注 0票数 4

我的应用程序中有一个提醒。其中我创建了每天重复的通知。现在可以在我的通知中心看到多个通知。由于它是可操作的通知,用户可以点击“是”或“否”。

我的监听器如下所示:

代码语言:javascript
复制
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")
        }

    }


}

我想读取通知日期(触发日期)。我发现了以下内容

代码语言:javascript
复制
let date = response.notification.date

但正如苹果的文档所说,它支持iOS 10及更高版本。但我想得到支持直到iOS7。

请参考通知中心图片,以便更清楚地了解。我有相同的标题,描述和所有通知信息相似。我想要检测哪个通知用户点击了。无论是周一的通知还是周二的通知。它是"A“还是"B”

EN

回答 1

Stack Overflow用户

发布于 2017-04-05 19:27:59

UILocalNotificationfireDate属性。你可以使用它。此外,要支持iOS7,您需要使用UILocalNotification而不是UNUserNotificationCenterDelegate

触发本地通知的步骤

代码语言:javascript
复制
let localNotification = UILocalNotification()
let fireDate = Date()
localNotification.fireDate = fireDate
localNotification.alertBody = "Your alert message"
localNotification.repeatInterval = .day
UIApplication.shared.scheduleLocalNotification(localNotification)

在你的应用委派中

代码语言:javascript
复制
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
  notification.fireDate
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43229674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档