簡體   English   中英

如何快速在每天的特定時間觸發本地通知3

[英]How to trigger a local notification at particular time daily in swift 3

我是iOS開發的新手,但是已經創建了該應用程序,並且嘗試創建特定時間(如(10AM))的每日通知。 現在,如果我將我的移動設備時間設置為上午10點,則通知將太多。 我只想在給定的特定時間(即上午10點)觸發一次本地通知,因此我想每天重復一次。 每天重復通知的最佳方法是什么?

這是我的代碼

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)    
}

在iOS 10的Swift 3中工作:

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)

你的問題是開火日期不合適

你可以像這樣創建射擊日期

    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)

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM