Reputation: 361
Hi I am trying to schedule a local notification to fire at 9:00am on weekdays (mon - fri) and cant seem to find any documentation on how to do this. Here is my code so far:
@IBAction func scheduleLocal(sender: UIButton) {
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return
}
if settings.types == .None {
let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
return
}
let notification = UILocalNotification()
notification.fireDate = NSDate()
notification.alertBody = "Come Exercise"
notification.alertAction = "Exercise Time"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["customField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
and in viewDidLoad:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
Upvotes: 0
Views: 1382
Reputation: 445
This is my class example you can use it as you want , i also support Arabic language by translate Arabic number to English .
//
// notification.swift
// NSFetchController Pro
//
// Created by Mr.Geeker on 08/02/15.
// Copyright (c) 2015 X2coder. All rights reserved.
//
import UIKit
class notification {
func createNotification(#hour:String, day:String, msgBody:String)->Bool
{
if(self.checkifNotificationOnOrOff())
{
//println("\(araToString(hour))")
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
var convertedTime:String = ""
var testDate:NSDate = NSDate()
var timeFormat:NSString = dateFormatter.stringFromDate(testDate)
// Get the range of date (AM OR PM )
var amRange:NSRange = timeFormat.rangeOfString(dateFormatter.AMSymbol)
var pmRange:NSRange = timeFormat.rangeOfString(dateFormatter.PMSymbol)
var is24h:Bool = (amRange.location == NSNotFound && pmRange.location == NSNotFound)
if is24h {
araToString(hour)
}else{
var dateFormatter2:NSDateFormatter = NSDateFormatter()
dateFormatter2.dateFormat = "HH:mm"
var hourDate:NSDate = dateFormatter2.dateFromString(hour)!
var pmAmDateString:NSString = dateFormatter2.stringFromDate(hourDate)
convertedTime = pmAmDateString
println(convertedTime)
}
var hourAndMiOnly:NSArray = convertedTime.componentsSeparatedByString(":")
var onlyHour:Int = 0
var onlyMinuts:Int = 0
for var i:Int = 0; i < 2; i++ {
for thimes in hourAndMiOnly
{
if i == 0
{
onlyMinuts = thimes.integerValue
}
if i == 1 {
// we should add -1 before the class
onlyHour = (thimes.integerValue)
if onlyHour < 1
{
onlyHour = 00
}
break
}
}
}
let DaysDic = [0:"None",1:"Sunday",2 :"Monday",3 :"Tuesday",4 :"Wednesday",5 :"Thursday",6 :"Friday",7 :"Saturday"]
var ConvertDayToInt:Int?{
for (key,value) in DaysDic
{
if value == day
{
return key
}
}
return nil
}
//println("Hour is \(onlyHour) And Minut \(onlyMinuts) AND Day \(ConvertDayToInt)")
let date = NSDate()
var userInfo: [NSObject : AnyObject]?
var createCalender:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var dateComponenet:NSDateComponents = createCalender.components(NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.WeekCalendarUnit, fromDate: date)
dateComponenet.weekday = ConvertDayToInt!
dateComponenet.hour = onlyHour
dateComponenet.minute = onlyMinuts
var fireDateToRun:NSDate = createCalender.dateFromComponents(dateComponenet)!
var notificationClass:UILocalNotification = UILocalNotification()
notificationClass.alertBody = msgBody
notificationClass.fireDate = fireDateToRun
notificationClass.soundName = UILocalNotificationDefaultSoundName
notificationClass.repeatInterval = NSCalendarUnit.WeekCalendarUnit
notificationClass.timeZone = NSTimeZone.defaultTimeZone()
notificationClass.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduledLocalNotifications = [notificationClass]
}else{
// call protocol function not support notification
}
return true
}
func araToString(numericString:String)->NSString
{
var nsMutString:NSMutableString = NSMutableString(string: numericString)
let ArabicString:NSString = "١٢٣٤٥٦٧٨٩:"
let EnglishString:NSString = "123456789:"
for ( var i:Int = 0 ; i < ArabicString.length ; i++)
{
var a:NSString = ArabicString.substringWithRange(NSMakeRange(i, 1))
var w:NSString = EnglishString.substringWithRange(NSMakeRange(i, 1))
nsMutString.replaceOccurrencesOfString(a, withString: w, options: NSStringCompareOptions.LiteralSearch, range:NSMakeRange(0, nsMutString.length))
}
return nsMutString
}
func deleteAll(){
let appnoti = UIApplication.sharedApplication()
let delArray = appnoti.scheduledLocalNotifications
if delArray.count > 0
{
appnoti.cancelAllLocalNotifications()
}
}
func checkifNotificationOnOrOff()->Bool
{
let systemVer:String = UIDevice.currentDevice().systemVersion
var finalFloat:Float = NSString(string: systemVer).floatValue
if finalFloat < 8 {
return false
}else{
return true
}
}
}
Upvotes: 0