iOS开发 iOS10以下推送

xiaoxiao2021-02-28  105

一.注册通知权限

1.iOS8以上:代码写在 AppDelegate 的 didFinishLaunchingWithOptions 方法中

let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: [userNotificationCategory]) application.registerUserNotificationSettings(settings) application.registerForRemoteNotifications()

2.iOS8以下:

application.registerForRemoteNotifications(matching: [.alert, .sound, .badge]) application.registerForRemoteNotifications()

二.本地通知

1.删除通知:

第一种:删除当前程序注册的所有通知,不重复的也会被取消

UIApplication.shared.cancelAllLocalNotifications()

第二种:删除指定的通知

for location in UIApplication.shared.scheduledLocalNotifications! { if location.category == "category" { UIApplication.shared.cancelLocalNotification(location) } }

2.创建通知

let localNotification = UILocalNotification() // 设置触发通知的时间 localNotification.fireDate = Date(timeIntervalSinceNow: 10) // 时区 localNotification.timeZone = TimeZone.current // 通知内容 localNotification.alertBody = "iOS10中废弃了UILocalNotification(UIKit Framework)这个类,采用了全新的UserNotifications Framework来推送通知,从此推送通知也有了自己的标签UN(这待遇真是没别人了),以及对推送功能的一系列增强改进(两个extension和界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点." if #available(iOS 8.2, *) { localNotification.alertTitle = "iOS 8.2标题" } // 设置启动图,点击推送通知打开app时显示的启动图片 localNotification.alertLaunchImage = "image" // 设置提醒的按钮文字/锁屏时界面底部的闪光文字(滑动来...) localNotification.alertAction = "自定义滑动解锁" // 角标 localNotification.applicationIconBadgeNumber = 1 // 通知的提示声音 localNotification.soundName = UILocalNotificationDefaultSoundName // 设置重复 localNotification.repeatInterval = .day // 通知参数 localNotification.userInfo = ["key": "iOS10学习"] // 设置分类,用于添加下拉快速回复功能 localNotification.category = "categoryIdentifier_iOS8" // 在哪个区域发送通知,进入这个区域就发送这个通知,可以进来调一次,出去调一次 localNotification.region = CLRegion() // 区域是否只检测一次 localNotification.regionTriggersOnce = true UIApplication.shared.scheduleLocalNotification(localNotification)

3.创建快捷回复功能:iOS8以后可以用

let userNotificationCategory = UIMutableUserNotificationCategory() userNotificationCategory.identifier = "categoryIdentifier_iOS8" // 设置前台按钮,点击后能使程序回到前台的叫做前台按钮 let foregroundAction = UIMutableUserNotificationAction() foregroundAction.identifier = "foreground" foregroundAction.title = "前台" foregroundAction.activationMode = .foreground // 设置后台按钮,点击后程序还在后台执行 let backgroundAction = UIMutableUserNotificationAction() backgroundAction.identifier = "background"; backgroundAction.activationMode = .background; backgroundAction.title = "后台"; userNotificationCategory.setActions([foregroundAction, backgroundAction], for: .default)

三.代理

1.点击通知调用:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 处理退出后通知的点击,程序启动后获取通知对象,如果是首次启动还没有发送通知,那第一次通知对象为空,没必要去处理通知(如跳转到指定页面) if ((launchOptions?[UIApplicationLaunchOptionsKey.localNotification]) != nil) { let localNotification = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as! UILocalNotification; self.changeLocalNotification(localNotification.userInfo) } return true }

func application(_ application: UIApplication, didReceive notification: UILocalNotification) { UIApplication.shared.applicationIconBadgeNumber = 0 self.changeLocalNotification(notification.userInfo) } //MARK: 处理接受到的通知 func changeLocalNotification(_ userInfo: Dictionary<AnyHashable, Any>?) { if (userInfo?.isEmpty)! { return } if UIApplication.shared.applicationState == .active { return } UIAlertView(title: String(), message: userInfo!["key"] as! String, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "确定", "取消").show() }

2.点击分类按钮调用:

// MARK: iOS10以下 点击分类按钮的回调 func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) { completionHandler() }

转载请注明原文地址: https://www.6miu.com/read-26424.html

最新回复(0)