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

を見つけしかし、Appleのドキュメントが言うように、それは、&後でiOSの10をサポートしています。しかし、私はiOS7までのサポートが欲しい。

enter image description here

明確にそれを理解するために、通知センターの画像を参照してください。 私は同じタイトル、説明&すべての通知情報に似ています。そして、どのユーザーがどの通知をタップしたのかを検出したい。天気は月曜日の通知か火曜日の通知でした。天気、それは "A" または "B" である

enter image description here

+0

ローカル通知を発射するUNUserNotificationCenterDelegate

の代わりにUILocalNotificationを使用する必要がiOS7をサポートします「UNUserNotificationCenterDelegate」を使用してください。これはiOS 10でのみ利用可能です。 –

+0

だから私を助けてください。 iOSの新機能です私は何を使うべきですか? –

+0

ユーザー "UILocalNotification"またはターゲットオーディエンスを変更します。 79%がios10にありますhttps://developer.apple.com/support/app-store/ –

答えて

0

UILocalNotificationfireDateプロパティがあります。あなたはそれを使うことができます。また、あなたはあなたはそれがiOSの7で仕事をしたいが、あなたはしている

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

いいえ、数日後に通知をタップすることはできません。しかし、ユーザーがそれとやり取りした日付は、実際にその通知の日付ではありません。ここ数日からそこにいた。じゃあ何???? –