我想为我的应用设置每日通知系统。它应该每天两次通知用户,一次是在上午8:00(嘿那里,你的早晨剂量准备好了。想看看吗?)和一次在晚上7点(Ssup!你的晚上Dose在里面等待) 。我知道通过这样做,我会在一个月内用完通知,因为有64个通知上限(对于本地通知)但到那时,应用程序将会生效并且我将完成设置远程通知更新。
我已经看过这个问题:How to schedule a same local notification in swift而且.Day都很好。我只需要知道如何在指定的时间每天做两次;提前谢谢!
编辑:如何设置通知的具体时间是问题的一部分。 NSDate API给了我有趣的时间间隔(从现在/自1973年起)。我只是想在晚上7点开火。 : - /似乎很难用这些NSDate apis来做,除非我遗漏了什么。
答案 0 :(得分:3)
针对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)")
}
}
}
答案 1 :(得分:2)
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
}