2016-11-10 1 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

가능 중복 ([SWIFT와 설정 시간에 매일 반복 지역 알림] 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) 

화재 날짜를 설정하는 시간이다. 스위프트 3 IO에서 10 작업

+0

매일 그것을 반복하는 방법 ?? –

+0

이하자 내 대답 –

+0

를 업데이트 오늘날 =있는 NSDate() 하자 일정 = NSCalendar.currentCalendar() 하자 구성 요소 =의 calendar.components ([일, .Month, .Year.] FROMDATE : 오늘) 하자 dateComp : NSDateComponents = NSDateComponents() dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 = 00 dateComp.minute 하자 날짜 = calendar.dateFromComponents (dateComp) 내가 이 코드를 변경했습니다. bez ur code dosent support swift3 이제 내 장치를 10AM으로 설정하면 3 개의 알림이 표시됩니다. –

0

:

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

관련 문제