2016-11-10 68 views
1

我是iOS開發新手,但已經創建了該應用程序,並且正在嘗試爲特定時間(例如(10AM))創建每日通知。現在,如果我的移動設備時間到達上午10點,通知就會變得太多。我想在給定的特定時間點上午10點觸發本地通知,我想每天重複一次。每天重複通知的最佳方法是什麼?如何在特定時間每天在特定時間觸發本地通知3

這裏是我的代碼

func fire(){ 
    let dateComp:NSDateComponents = NSDateComponents() 
    dateComp.hour = 10 
    dateComp.minute = 00 
    dateComp.timeZone = NSTimeZone.systemTimeZone() 
    let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIndian)! 
    let date:NSDate = calender.dateFromComponents(dateComp)! 
    let localNotificationSilent = UILocalNotification() 
    localNotificationSilent.fireDate = date 
    localNotificationSilent.repeatInterval = NSCalendarUnit.Day 
    localNotificationSilent.alertBody = "Started!" 
    localNotificationSilent.alertAction = "swipe to hear!" 
    localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone 
    localNotificationSilent.category = "PLAY_CATEGORY" 
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationSilent)  
} 
+0

的可能的複製[在設定的時間以快速重複每日本地通知(http://stackoverflow.com/questions/30619998/repeating-local-通知 - 每天一個 - 設置 - 與 - 迅速) – sanman

回答

1

你的問題是消防日這是不正確的

您可以創建火日期一樣,如果你想驗證火災日期這樣

let today = Date() 
    let calendar = NSCalendar.current 
    let components = calendar.dateComponents([.day, .month, .year], from: today) 

    var dateComp:DateComponents = DateComponents() 
    dateComp.day = components.day 
    dateComp.month = components.month 
    dateComp.year = components.year 
    dateComp.hour = 10 
    dateComp.minute = 00 
    let date = calendar.date(from: dateComp) 

那麼你可以這樣檢查

let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss" 
    let fireDate = dateFormatter.string(from: date!) 
    print("fireDate: \(fireDate)") 

現在是時候火日期設置爲本地通知

localNotificationSilent.fireDate = date 
// no need to set time zone Remove bellow line 
localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone 

通知碼

let localNotificationSilent = UILocalNotification() 
    localNotificationSilent.fireDate = date 
    localNotificationSilent.repeatInterval = .day 
    localNotificationSilent.alertBody = "Started!" 
    localNotificationSilent.alertAction = "swipe to hear!" 
    localNotificationSilent.category = "PLAY_CATEGORY" 
    UIApplication.shared.scheduleLocalNotification(localNotificationSilent) 

我希望這會幫助你。

+0

如何每天重複? –

+0

更新我的回答 –

+0

讓今天= NSDate的() 讓日曆= NSCalendar.currentCalendar() 讓組件= calendar.components([日,.Month,.Year。] FROM日期:今天) 讓dateComp:NSDateComponents = NSDateComponents() dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 dateComp.minute = 00 令日期= calendar.dateFromComponents(dateComp)我已經改變了這樣的代碼bez你的代碼dosent支持swift3,現在得到3通知,如果我把我的設備設置爲10AM –

0

在夫特3,iOS的10個工作:

UNUserNotificationCenter.current().requestAuthorization(
    options: [.alert, .sound, .badge], completionHandler: {userDidAllow, error in 
    //if userDidAllow : do something if you want to 
}) 

//Set notification to trigger 11:30AM everyday 
var dateComponents = DateComponents() 
dateComponents.hour = 11 
dateComponents.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 

//Set your content 
let content = UNMutableNotificationContent() 
content.title = "Your notification title" 
content.body = "Your notification body" 

let request = UNNotificationRequest(
    identifier: "yourIdentifier", content: content, trigger: trigger 
) 
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
+0

嘿,我不能夠在特定的時間開火通知。我做了相同的代碼。 – Abha

相關問題