2017-08-28 16 views
0

ローカル通知を送信しようとしています。私のコードは以下のとおりです。 問題通知は起動しません。私はstackoverflowですべての可能な答えを試みた。提案してください。 は私がやった仕上げ起動上のすべての通知をオフにしても、通知、私は解決通知が特定の時刻に発生しない

func applicationWillTerminate(_ application: UIApplication) { 
    self.loadData() 
    for obj in eventArray 
    { 
     if (obj["notification"] as? Bool)! 
     { 
      scheduleNotification(obj["time"] as! Date, obj["title"] as! String) 
      print(obj["time"] as! Date) 
     } 
    } 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 
func scheduleNotification(_ date: Date, _ title:String) { 
    let formatter = DateFormatter() 
    formatter.dateFormat = "HH" 
    let myhour = formatter.string(from: date) 
    let number = NumberFormatter() 
    let mynumberhour = number.number(from: myhour) 
    let intHour = mynumberhour?.intValue 
    print(intHour!) 

    formatter.dateFormat = "mm" 
    let myminute = formatter.string(from: date) 
    let mynumberMinute = number.number(from: myminute) 
    let intMinute = mynumberMinute?.intValue 
    print(intMinute!) 

    formatter.dateFormat = "MM" 
    let mymonth = formatter.string(from: date) 
    let mynumberMonth = number.number(from: mymonth) 
    let intMonth = mynumberMonth?.intValue 
    print(intMonth!) 

    formatter.dateFormat = "dd" 
    let myday = formatter.string(from: date) 
    let mynumberday = number.number(from: myday) 
    let intday = mynumberday?.intValue 
    print(intday!) 
    let calendar = Calendar.current 
    //let components = calendar.dateComponents(in: .current, from: date) 
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: intMonth, day: intday, hour: intHour, minute: intMinute) 

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true) 
    let content = UNMutableNotificationContent() 
    content.title = " Today's Event" 
    content.body = "Event Is Generated" 
    content.sound = UNNotificationSound.default() 
    let request = UNNotificationRequest(identifier: title, content: content, trigger: trigger) 
    UNUserNotificationCenter.current().add(request) {(error) in 
     if let error = error { 
     print("Uh oh! We had an error: \(error)") 
     } 
    } 
} 
+0

'identifier'パラメータに渡す値は? – Basheer

答えて

0

問題のためのユーザー権限を設定します。 "func applicationDidEnterBackground"で "func scheduleNotification"と呼ばれ、通知が正常に呼び出されました。

関連する問題