【问题标题】:How to detect triggered date of an actionable Notification如何检测可操作通知的触发日期
【发布时间】:2017-08-30 23:29:05
【问题描述】:

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

我的监听器如下:

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 中工作,但您使用的 UNUserNotificationCenterDelegate 仅在 iOS 10 中可用。
  • 所以请帮助我。我是 iOS 新手。我应该使用什么。
  • 用户UILocalNotification 或更改您的目标受众。 79% 在 ios10 developer.apple.com/support/app-store

标签: ios objective-c swift swift3 swift2


【解决方案1】:

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

触发本地通知

let localNotification = UILocalNotification()
let fireDate = Date()
localNotification.fireDate = fireDate
localNotification.alertBody = "Your alert message"
localNotification.repeatInterval = .day
UIApplication.shared.scheduleLocalNotification(localNotification)

在您的应用委托中

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
  notification.fireDate
}

【讨论】:

  • 我无法设置 ID bcz 我想在用户点击时读取日期。但我什至不能设置日期,因为它是每日通知。
  • 您想查看触发通知的日期或用户点击您的通知的时间?您可以获取调用方法的当前时间,即用户点击的时间
  • 不,这是错误的 bcz 用户可以在几天后点击通知。但是用户与之交互的日期并不是该通知的实际触发日期。从最近几天开始就在那里。然后呢????
  • 您想要通知的触发日期,然后在触发时将该日期放入通知中。
  • @RAISIQBAL 您只需要使用UILocalNotification 并实现委托方法application(_ application: UIApplication, didReceive notification: UILocalNotification) 并访问本地通知的fireDate 属性
猜你喜欢
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
相关资源
最近更新 更多