Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

background - IOS Local Notification at specific time

Below is what I am trying to achieve. Show local notification to the user about say sunrise/sunset time. Say sunrise time is at 05:30AM and sunset time at 06:30PM. Current time is 07:00PM, when the user enables the notification feature then every one hour show sunrise notification with info time left for sunrise ( that is x hours and y mins left) Once the time left for sunrise is less than 60 mins, then change the interval from one hour to say 10 mins. Finally when the time left for sunrise is less than 10 mins, then show notification for every minute. When the time for sunrise is reached repeat the same for sunset. This goes on till the user disable this feature via app settings.

Showing a countdown timer is also fine with in a notification if that doesn't drain the battery.

What I have achieved is fire a local notification at a specific time. Appreciate any guidance. I am new to IOS development.

let center = UNUserNotificationCenter.current()
      let content = UNMutableNotificationContent()
      content.title = title
      content.body = body
      content.categoryIdentifier = "sdf"
      
      if let info = userInfo {
        content.userInfo = info
      }
      
      content.sound = UNNotificationSound.default()
      
      var comp = Calendar.current.dateComponents([.hour, .minute], from: date)
      comp.minute = comp.minute! + 2
      
      let trigger = UNCalendarNotificationTrigger(dateMatching: comp, repeats: false)
      
      //let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
      
      let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
      
      center.add(request)
question from:https://stackoverflow.com/questions/65898782/ios-local-notification-at-specific-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...