作者:有些疯癫的小红帽 | 来源:互联网 | 2023-01-17 12:46
imanoobtoprogrammingonswift,andimfacingaproblem.Imtryingtoschedulealocalnotific
i'm a noob to programming on swift, and i'm facing a problem. I'm trying to schedule a local notification every day at the same hour, this without the user scheduling it. Some how the repeat interval method gives me more that one notification at the time. So i don't now what's happening. Basically what i want is to schedule the notification properly and call the schedule method in the right way.
我是一个快速编程的菜鸟,我正面临着一个问题。我正在尝试每天在同一时间安排本地通知,这没有用户安排它。一些重复间隔方法如何为我提供更多的通知。所以我现在不知道发生了什么。基本上我想要的是正确安排通知并以正确的方式调用调度方法。
This is my view controller method where i call the notification
这是我的视图控制器方法,我调用通知
override func viewDidLoad() {
super.viewDidLoad()
notificar.scheduleNotification("message")
}
This is the notification helper class
这是通知助手类
class NotificationHelper {
static func askPermission() {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
func scheduleNotification(InputUser:String) {
let now: NSDateCompOnents= NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: NSDate())
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let date = cal.dateBySettingHour(now.hour, minute: now.minute + 1, second: 0, ofDate: NSDate(), options: NSCalendarOptions())
let reminder = UILocalNotification()
reminder.fireDate = date
reminder.alertBody = InputUser
reminder.alertAction = "Cool"
reminder.soundName = "sound.aif"
reminder.repeatInterval = NSCalendarUnit.Minute
UIApplication.sharedApplication().scheduleLocalNotification(reminder)
print("Firing at \(now.hour):\(now.minute+1)")
}
And this is my app delegate
这是我的应用代表
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
NotificationHelper.askPermission()
return true
}
As you can see, in the code im calling every minute the notification repeat interval, but as soon as the notification gets called i recive an stack of 5 notifications in a row, not single one.
正如你所看到的,在代码中我每分钟都会调用通知重复间隔,但是一旦调用了通知,我就会连续收到一堆5个通知,而不是单个通知。
please any ideas, i will be very grateful. And sorry for my english. thanks :)
请任何想法,我将非常感激。抱歉我的英语。谢谢 :)
1 个解决方案