2017-04-05 51 views
4

我的应用程序中有提醒。我在其中创建了每日重复的通知。现在我的通知中心可以看到多个通知。而且由于它是可操作的通知用户可以点击“是”或“否”。如何检测可执行通知的触发日期

我已经监听它如下:

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。

enter image description here

请参阅通知中心图像清楚地了解它。 我有同样的标题,说明&所有通知信息相似。我想检测哪个通知用户点击。天气是周一的通知或周二的通知。天气是 “A” 或 “B”

enter image description here

+0

你希望它在iOS的7工作,但你使用仅适用于iOS 10的'UNUserNotificationCenterDelegate'。 –

+0

请帮助我。我是iOS新手。我应该使用什么。 –

+0

用户“UILocalNotification”或更改您的目标受众。 79%在ios10上https://developer.apple.com/support/app-store/ –

回答

0

还有就是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 
} 
+0

我无法设置ID bcz我想在用户点击时阅读日期。但我甚至无法设置日期bcz这是每日通知。 –

+1

你想读取通知的日期或者当用户点击你的通知?你可以获得当你的方法被调用的时间,这是用户点击的时间 –

+0

不,它是不可能的bcz用户可以在几天后点击通知。但用户与其互动的日期,并不是实际开通日期的通知。它在过去几天以来一直在那里。那么什么? –