iOS swift-发起本地通知,设置通知语音

//MARK:发起通知

func scheduleNotification(itemID:Int){

//如果已存在该通知消息,则先取消

cancelNotification(itemID: itemID)

//创建UILocalNotification来进行本地消息通知

let localNotification = UILocalNotification()

//推送时间(设置为30秒以后)

localNotification.fireDate = Date(timeIntervalSinceNow: 6)

//时区

localNotification.timeZone = NSTimeZone.default

//推送内容

localNotification.alertBody = "您已超出围栏区域"

// 2.7.设置有通知时的音效

localNotification.soundName = "126.wav"

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

//声音

//        localNotification.soundName = UILocalNotificationDefaultSoundName

//额外信息

localNotification.userInfo = ["ItemID":itemID]

UIApplication.shared.scheduleLocalNotification(localNotification)

}

//取消通知消息

func cancelNotification(itemID:Int){

//通过itemID获取已有的消息推送,然后删除掉,以便重新判断

let existingNotification = self.notificationForThisItem(itemID: itemID)

if existingNotification != nil {

//如果existingNotification不为nil,就取消消息推送

UIApplication.shared.cancelLocalNotification(existingNotification!)

}

}

//通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification

func notificationForThisItem(itemID:Int)-> UILocalNotification? {

let allNotifications = UIApplication.shared.scheduledLocalNotifications

for notification in allNotifications! {

let info = notification.userInfo as! [String:Int]

let number = info["ItemID"]

if number != nil && number == itemID {

return notification as UILocalNotification

}

}

return nil

}

//MARK:发起通知

func FaQiTongZhi() -> () {

// 1.创建本地通知

let localNote = UILocalNotification()

// 2.设置本地通知的内容

// 2.1.设置通知发出的时间

localNote.fireDate! = NSDate(timeIntervalSinceNow: 1.0) as Date

// 2.2.设置通知的内容

localNote.alertBody! = "超出围栏"

// 2.3.设置滑块的文字(锁屏状态下:滑动来“解锁”)

localNote.alertAction! = "解锁"

// 2.4.决定alertAction是否生效

localNote.hasAction = false

// 2.5.设置点击通知的启动图片

localNote.alertLaunchImage! = "icon_huodong"

// 2.6.设置alertTitle

if #available(iOS 8.2, *) {

localNote.alertTitle = "你有一条新通知"

} else {

// Fallback on earlier versions

//            localNote.alertBody = "你有一条新通知"

}

// 2.7.设置有通知时的音效

localNote.soundName! = "8308.wav"

// 2.8.设置应用程序图标右上角的数字

localNote.applicationIconBadgeNumber = 1

// 2.9.设置额外信息

localNote.userInfo! = ["type": 1]

// 3.调用通知

UIApplication.shared.scheduleLocalNotification(localNote)

}

你可能感兴趣的:(iOS swift-发起本地通知,设置通知语音)