在一天中的特定时间安排本地通知
Schedule Local Notifications at Specific times in the day
我想为我的应用设置每日通知系统。它应该每天两次通知用户,一次在 8:00 AM(嘿,你的早上剂量准备好了。想检查一下吗?)一次在 7:00 PM(Ssup!你晚上的剂量正在里面等着)。我知道通过这样做我会 运行 在一个月内没有通知,因为有 64 个通知上限(对于本地通知)但是到那时,该应用程序将上线并且我将完成远程通知的设置更新。
我看过这个问题:How to schedule a same local notification in swift .Day 很好。我只需要知道如何在指定的时间每天做两次;。提前致谢!
编辑:如何设置通知的具体时间是问题的一部分。 NSDate API 给了我有趣的时间间隔 (sinceNow/Since1973)。我只想将其设置为在晚上 7 点开火。 :-/ 除非我遗漏了什么,否则似乎无法使用这些 NSDate api 来完成。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message 111"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getEightAMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
let localNotification2 = UILocalNotification()
localNotification2.alertBody = "Your alert message22"
localNotification2.timeZone = NSTimeZone.defaultTimeZone()
localNotification2.fireDate = self.getSevenPMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)
return true
}
func getEightAMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date10h
}
func getSevenPMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date19h
}
更新 Swift 4.2
import UserNotifications
在 AppDelegate 中确认 UNUserNotificationCenterDelegate
并在 didFinishLaunchingWithOptions
let center = UNUserNotificationCenter.current()
center.delegate = self;
center.requestAuthorization(options: [UNAuthorizationOptions.alert, .badge, .sound]) { (granted, error) in
if !granted {
print("Permission Declined");
}
}
let content = UNMutableNotificationContent();
content.title = "HI";
content.body = "Your notification is here";
content.sound = UNNotificationSound.default;
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian);
let now = Date();
var components = gregorian.dateComponents(in: .autoupdatingCurrent, from: now)
let hours = [8,19];
for hour in hours {
components.timeZone = TimeZone.current
components.hour = hour;
components.minute = 00;
components.second = 00;
let date = gregorian.date(from: components);
let formatter = DateFormatter();
formatter.dateFormat = "MM-dd-yyyy HH:mm";
guard let dates = date else {
return;
}
var fireDate: String?
fireDate = formatter.string(from: dates);
print("\(fireDate ?? "")"); // Just to Check
let dailyTrigger = Calendar.current.dateComponents([.hour, .minute, .second], from: dates);
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dailyTrigger, repeats: true);
let identifier = "Local Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request) { (error) in
if let error = error {
print("Error \(error.localizedDescription)")
}
}
}
我想为我的应用设置每日通知系统。它应该每天两次通知用户,一次在 8:00 AM(嘿,你的早上剂量准备好了。想检查一下吗?)一次在 7:00 PM(Ssup!你晚上的剂量正在里面等着)。我知道通过这样做我会 运行 在一个月内没有通知,因为有 64 个通知上限(对于本地通知)但是到那时,该应用程序将上线并且我将完成远程通知的设置更新。
我看过这个问题:How to schedule a same local notification in swift .Day 很好。我只需要知道如何在指定的时间每天做两次;。提前致谢!
编辑:如何设置通知的具体时间是问题的一部分。 NSDate API 给了我有趣的时间间隔 (sinceNow/Since1973)。我只想将其设置为在晚上 7 点开火。 :-/ 除非我遗漏了什么,否则似乎无法使用这些 NSDate api 来完成。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message 111"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getEightAMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
let localNotification2 = UILocalNotification()
localNotification2.alertBody = "Your alert message22"
localNotification2.timeZone = NSTimeZone.defaultTimeZone()
localNotification2.fireDate = self.getSevenPMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)
return true
}
func getEightAMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date10h
}
func getSevenPMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date19h
}
更新 Swift 4.2
import UserNotifications
在 AppDelegate 中确认 UNUserNotificationCenterDelegate
并在 didFinishLaunchingWithOptions
let center = UNUserNotificationCenter.current()
center.delegate = self;
center.requestAuthorization(options: [UNAuthorizationOptions.alert, .badge, .sound]) { (granted, error) in
if !granted {
print("Permission Declined");
}
}
let content = UNMutableNotificationContent();
content.title = "HI";
content.body = "Your notification is here";
content.sound = UNNotificationSound.default;
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian);
let now = Date();
var components = gregorian.dateComponents(in: .autoupdatingCurrent, from: now)
let hours = [8,19];
for hour in hours {
components.timeZone = TimeZone.current
components.hour = hour;
components.minute = 00;
components.second = 00;
let date = gregorian.date(from: components);
let formatter = DateFormatter();
formatter.dateFormat = "MM-dd-yyyy HH:mm";
guard let dates = date else {
return;
}
var fireDate: String?
fireDate = formatter.string(from: dates);
print("\(fireDate ?? "")"); // Just to Check
let dailyTrigger = Calendar.current.dateComponents([.hour, .minute, .second], from: dates);
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dailyTrigger, repeats: true);
let identifier = "Local Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request) { (error) in
if let error = error {
print("Error \(error.localizedDescription)")
}
}
}