在视图 did load 方法中安排本地通知
Schedule local notifications in the view did load method
我是 swift 上的编程新手,我遇到了一个问题。我正在尝试每天在同一时间安排本地通知,而无需用户安排。一些重复间隔方法如何在当时给我更多的通知。所以我现在不知道发生了什么。基本上我想要的是正确安排通知并以正确的方式调用计划方法。
这是我调用通知的视图控制器方法
override func viewDidLoad() {
super.viewDidLoad()
notificar.scheduleNotification("message")
}
这是通知助手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)")
}
这是我的应用委托
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
NotificationHelper.askPermission()
return true
}
如您所见,在代码中,我每分钟调用一次通知重复间隔,但一旦调用通知,我就会连续收到一堆 5 个通知,而不是一个。
请提出任何想法,我将不胜感激。对不起我的英语。
谢谢:)
你必须先删除本地通知,然后再添加一个新的,这里是代码。
var app: UIApplication = UIApplication.sharedApplication()
var eventArray: [AnyObject] = app.scheduledLocalNotifications()
for oneEvent: UILocalNotification in eventArray
{
var userInfoCurrent: [NSObject : AnyObject] = oneEvent.userInfo
var uid: String = "\(userInfoCurrent["name"])"
// here give the name of your local notification.
if (uid == obj["name"])
{
//Cancelling local notification
app.cancelLocalNotification(oneEvent)
}
}
我是 swift 上的编程新手,我遇到了一个问题。我正在尝试每天在同一时间安排本地通知,而无需用户安排。一些重复间隔方法如何在当时给我更多的通知。所以我现在不知道发生了什么。基本上我想要的是正确安排通知并以正确的方式调用计划方法。
这是我调用通知的视图控制器方法
override func viewDidLoad() {
super.viewDidLoad()
notificar.scheduleNotification("message")
}
这是通知助手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)")
}
这是我的应用委托
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
NotificationHelper.askPermission()
return true
}
如您所见,在代码中,我每分钟调用一次通知重复间隔,但一旦调用通知,我就会连续收到一堆 5 个通知,而不是一个。
请提出任何想法,我将不胜感激。对不起我的英语。 谢谢:)
你必须先删除本地通知,然后再添加一个新的,这里是代码。
var app: UIApplication = UIApplication.sharedApplication()
var eventArray: [AnyObject] = app.scheduledLocalNotifications()
for oneEvent: UILocalNotification in eventArray
{
var userInfoCurrent: [NSObject : AnyObject] = oneEvent.userInfo
var uid: String = "\(userInfoCurrent["name"])"
// here give the name of your local notification.
if (uid == obj["name"])
{
//Cancelling local notification
app.cancelLocalNotification(oneEvent)
}
}