2017-04-05 62 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用戶可以在幾天後點擊通知。但用戶與其互動的日期,並不是實際開通日期的通知。它在過去幾天以來一直在那裏。那麼什麼? –