一、推送
1.本地推送
(1)iOS8.0+
--------------------------AppDelegate----------------------------
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print(#function)
//本地推送请求授权
registerAuthor()
// 当app退出时,点击本地通知启动app,判断是否点击本地通知启动的,从而获取本地通知
if launchOptions != nil {
let localNotice = launchOptions![UIApplicationLaunchOptionsKey.localNotification]
print(localNotice!)
}
return true
}
/**
* 推送通知发送权限:
* 如果是iOS8.0以前, 直接通过以下代码就可以发送
* 如果是iOS8.0之后, 需要主动的向用户请求授权
*/
func registerAuthor() {
if #available(iOS 8.0, *){
let types = (UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.alert.rawValue)
let settings = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: types), categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
}
// 接收到本地通知, 并且满足以下条件时调用
// 1. APP 在前台 (不会弹框)
// 2. APP 在后台, 点击推送进入前台
// 3. APP 锁屏, 点击推送进入前台
// 注意: App退出时, 不调用此方法
func application(_ application: UIApplication, didReceive:UILocalNotification ){
print("接收本地通知")
if application.applicationState == .active{
print("app当前处于前台状态")
}else if application.applicationState == .inactive{
print("app从后台进入前台状态")
}
}
------------------------------ViewController.Swift-------------------------------
import UIKit
class ViewController: UIViewController {
@IBAction func sendNotification(_ sender: UIButton) {
// 1. 创建一个本地通知
let localNotice = UILocalNotification()
// 1.1 设置通知必选项
// 1.1.1 设置通知发送时间
localNotice.fireDate = NSDate(timeIntervalSinceNow: 5) as Date
localNotice.timeZone = NSTimeZone.default
// 1.1.2 设置通知内容
localNotice.alertBody = "王二小在山边吃草"
// 1.2 设置通知附加选项
// 1.2.1 设置通知重复周期(间隔至少1分钟)
// localNotice.repeatInterval = NSCalendarUnit.Minute
// 1.2.2 设置滑动文字(锁屏界面上的"滑动来" + alertAction内容)
localNotice.alertAction = "决定"
localNotice.hasAction = true
// 1.2.3 设置启动图片
localNotice.alertLaunchImage = nil
// 1.2.4 设置通知标题
if #available(iOS 8.2, *) {
localNotice.alertTitle = "斗地主"
}
// 1.2.5 设置通知提示音
localNotice.soundName = "buyao.wav"
// 1.2.6 设置提示图标
localNotice.applicationIconBadgeNumber = 1
// 1.2.7 设置附加信息(用于数据传递)
localNotice.userInfo = ["name": "shunzi", "age": 10]
// 1.3 设置附加选项
if #available(iOS 8.0, *) {
localNotice.category = "select"
} else {
// Fallback on earlier versions
}
// 2. 发送一个本地通知
// 立即发送
// UIApplication.sharedApplication().presentLocalNotificationNow(localNotice)
// 根据计划时间, 计划发送一个通知
UIApplication.shared.scheduleLocalNotification(localNotice)
}
@IBAction func cancelNotification(_ sender: UIButton) {
//取消所有本地通知
UIApplication.shared.cancelAllLocalNotifications()
//取消某一个本地通知
// UIApplication.shared.cancelLocalNotification(<#T##notification: UILocalNotification##UILocalNotification#>)
}
@IBAction func lookNotification(_ sender: UIButton) {
let localNotis = UIApplication.shared.scheduledLocalNotifications
print(localNotis!)
}
}
(2)设置附加行为选项
/**
* 推送通知发送权限:
* 如果是iOS8.0以前, 直接通过以下代码就可以发送
* 如果是iOS8.0之后, 需要主动的向用户请求授权
*/
func registerAuthor() {
if #available(iOS 8.0, *){
//1.创建一个操作选项组
let category: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
//1.1每一组的标识
category.identifier = "select"
//1.2设置组的操作行为
//1.2.1设置一个行为
let action1: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
//行为的标识,区别用户点击的是哪一个行为
action1.identifier = "shunzi"
//行为名称
action1.title = "顺子"
//设置行为执行的环境
//Foreground 意味着用户点击了这个按钮,就会进入前台,执行对应的行为
//Background 意味着用户点击了这个按钮,在后台,就可以执行对应的行为
action1.activationMode = .foreground
//设置是否需要必须解锁才能使用
//如果activationMode设置是前台,则这个属性会被忽略
action1.isAuthenticationRequired = true
//设置是否是一个破坏性行为(通过一些颜色,来标识这个按钮,惊醒用户)
action1.isDestructive = true;
//修改行为的样式
if #available(iOS 9.0, *) {
action1.behavior = .textInput
//设置行为参数
action1.parameters = [UIUserNotificationTextInputActionButtonTitleKey: "压死"]
}
//1.2.2 设置另一个行为
let action2: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
//行为的标识,区别用户点击的是哪一个行为
action2.identifier = "wangzha"
//行为名称
action2.title = "王炸"
//设置行为执行的环境
//Foreground 意味着用户点击了这个按钮,就会进入前台,执行对应的行为
//Background 意味着用户点击了这个按钮,在后台,就可以执行对应的行为
action2.activationMode = .background
//设置是否需要必须解锁才能使用
//如果activationMode设置是前台,则这个属性会被忽略
action2.isAuthenticationRequired = true
//设置是否是一个破坏性行为(通过一些颜色,来标识这个按钮,惊醒用户)
action2.isDestructive = true;
//1.3 行为组
let actions:[UIUserNotificationAction] = [action1,action2]
//Default 默认只能添加4个行为
//Minal 默认只能添加2个行为
category.setActions(actions, for: UIUserNotificationActionContext.default)
//2.创建一个操作选项组集合
let categoriesSet: Set<UIUserNotificationCategory> = [category]
//3.设置请求的权限是什么
let types = (UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.alert.rawValue)
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: types), categories: categoriesSet)
UIApplication.shared.registerUserNotificationSettings(settings)
}
}
//iOS8.0+
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
print("iOS8.0+")
completionHandler()
}
//iOS9.0+
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
if identifier == "shunzi" {
let str: String = responseInfo[UIUserNotificationActionResponseTypedTextKey] as! String
print("使用顺子"+str+"压死")
}else if identifier == "wangzha"{
print("炸死")
}
completionHandler()
}
网友评论