2017-04-05 1 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 

을 발견하지만 사과 문서 말한다으로는 & 나중에 아이폰 OS 10을 지원합니다. 하지만 iOS7까지 지원이 필요합니다.

enter image description here

명확하게 이해하는 알림 센터 이미지를 참조하시기 바랍니다. 동일한 제목, 설명 & 모든 알림 정보가 비슷합니다. 사용자가 어떤 알림을 탭했는지 감지하려고합니다. 날씨는 월요일 통보 또는 화요일 통보였습니다. 날씨가 "A"또는 "B"입니다

enter image description here

+0

앱 위임에서 로컬 통지를

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

을 해고하는 대신 UNUserNotificationCenterDelegate

UILocalNotification를 사용해야하지만 당신은있어 iOS 10에서만 사용할 수있는 'UNUserNotificationCenterDelegate'를 사용하십시오. –

+0

제발 도와주세요. iOS에 익숙하지 않습니다. 무엇을 사용해야합니까? –

+0

사용자 'UILocalNotification'또는 타겟 고객을 변경하십시오. 79 %가 ios10에 있습니다. https://developer.apple.com/support/app-store/ –

답변

0

UILocalNotificationfireDate 특성이있다. 당신이 그것을 사용할 수 있습니다. 또한 iOS7에를 지원하기 위해 당신은 당신이 아이폰 OS 7에서 작업 할

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

ID를 설정할 수 없습니다. bcz 사용자가 탭할 때 날짜를 읽고 싶습니다. 그러나 나는 심지어 날짜 bcz를 설정할 수 없습니다. 매일 알림입니다. –

+1

해고 통보 날짜 또는 사용자가 통보 한 날짜를 읽고 싶습니까? 사용자가 메소드를 호출 할 때 현재 시간을 얻을 수 있습니다. 즉, 사용자가 도청하는 시간입니다. –

+0

아니요, bcz 사용자는 며칠 후에 알림을 누를 수 있습니다. 그러나 사용자가 상호 작용 한 날짜는 해당 알림 날짜에 실제 해고 된 날짜가 아닙니다. 지난 며칠간 거기에있었습니다. 그리고 뭐???? –

관련 문제