Ios 在view Dod load方法中安排本地通知
我是swift编程的新手,我面临一个问题。我尝试在每天的同一时间安排一个本地通知,而不让用户安排它。重复间隔方法如何在同一时间为我提供多个通知。所以我现在不知道发生了什么。基本上,我想要的是正确地安排通知,并以正确的方式调用schedule方法 这是我调用通知的视图控制器方法Ios 在view Dod load方法中安排本地通知,ios,iphone,swift,notifications,ios9,Ios,Iphone,Swift,Notifications,Ios9,我是swift编程的新手,我面临一个问题。我尝试在每天的同一时间安排一个本地通知,而不让用户安排它。重复间隔方法如何在同一时间为我提供多个通知。所以我现在不知道发生了什么。基本上,我想要的是正确地安排通知,并以正确的方式调用schedule方法 这是我调用通知的视图控制器方法 override func viewDidLoad() { super.viewDidLoad() notificar.scheduleNotification("message")
override func viewDidLoad() {
super.viewDidLoad()
notificar.scheduleNotification("message")
}
这是通知助手类
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)
}
}
根据代码,每次加载视图控制器时都会创建一个新通知。您必须在第一次计划通知时设置一个标志(并将其持久化),以便在计划新通知之前设置一次通知或取消所有通知。此外,您的重复间隔是NSCalendarUnit.Minute
而不是NSCalendarUnit.Day