SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。
1.首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject]?) -> Bool { if (UIDevice.currentDevice().systemVersion as NSString ).floatValue >= 8 { //APService.registerForRemoteNotificationTypes( //UIUserNotificationType.Badge.rawValue | //UIUserNotificationType.Sound.rawValue | //UIUserNotificationType.Alert.rawValue, //categories: nil) application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil )) } //APService.setupWithOption(launchOptions) return true } |
2.有几个方法要说一下,
1.func applicationWillResignActive(application: UIApplication){} 当App既将进入后台、锁屏、有电话进来时会触发此事件
2.func applicationDidEnterBackground(application: UIApplication) {} 当App进入后台时触发此事件
3.func applicationWillEnterForeground(application: UIApplication) {} 当App从后台即将回到前台时触发此事件
4.func applicationDidBecomeActive(application: UIApplication) {}当App变成活动状态时触发此事件
5.func applicationWillTerminate(application: UIApplication) {} 当App退出时触发此方法,一般用于保存某些特定的数据
此时在applicationDidEnterBackground方法内写入以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. UIApplication.sharedApplication().cancelAllLocalNotifications() var notification = UILocalNotification() //notification.fireDate = NSDate().dateByAddingTimeInterval(1) //setting timeZone as localTimeZone notification.timeZone = NSTimeZone .localTimeZone() notification.repeatInterval = NSCalendarUnit .CalendarUnitDay notification.alertTitle = "This is a local notification" notification.alertBody = "Hey,It's great to see you again" notification.alertAction = "OK" notification.soundName = UILocalNotificationDefaultSoundName //setting app's icon badge notification.applicationIconBadgeNumber = 1 var userInfo:[ NSObject : AnyObject] = [ NSObject : AnyObject]() userInfo[ "kLocalNotificationID" ] = "LocalNotificationID" userInfo[ "key" ] = "Attention Please" notification.userInfo = userInfo //UIApplication.sharedApplication().scheduleLocalNotification(notification) //UIApplication.sharedApplication().presentLocalNotificationNow(notification) application.presentLocalNotificationNow(notification) } |
此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”
3.当用户点击消息时会触发didReceiveLocalNotification事件,在这个事件内写些代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { UIApplication.sharedApplication().cancelAllLocalNotifications() let userInfo = notification.userInfo! let title = userInfo[ "key" ] as! String var alert = UIAlertView() alert.title = title alert.message = notification.alertBody alert.addButtonWithTitle(notification.alertAction!) alert.cancelButtonIndex = 0 alert.show() //APService.showLocalNotificationAtFront(notification, identifierKey: nil) } |
4.当程序处于活动状态的时候清除ICON的角标
1
2
3
4
5
6
7
8
|
func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. //setting the desk top application icon's badge as zero //application.applicationIconBadgeNumber = 0 application.cancelAllLocalNotifications() application.applicationIconBadgeNumber = 0 } |
相关文章
- 【亲测可访问】外网如何访问局域网本地搭建的服务器
- python项目模块打包本地发布并上传到到PyPI官网
- SpringBoot:上传单个图片,上传图片压缩包,读取本地磁盘图片
- git如何删除远端不存在的本地分支?
- Swift 2.x 升为 swift 3后语法不兼容问题适配
- Swift 1.1语言函数参数的特殊情况本地参数名外部参数名
- Windows系统Maven下载 环境变量配置 阿里云镜像配置 本地仓库搭建 开发环境搭建一次解决
- vue只能本地跨域,线上跨域要后端弄
- scratch-blocks在Mac下本地编译
- Git分支(本地)
- Swift 本地推送通知UILocalNotification
- swift 深入理解Swift的闭包
- Swift - 网页控件(UIWebView)加载本地数据,文件
- 配置Yum源repo文件及搭建本地Yum服务器
- 下载安装tomcat 部署本地项目
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
- C# 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化
- 本地Navicat远程连接Centos7服务器出现的错误汇总
- node中一个基本的HTTP客户端向本地的HTTP服务器发送数据
- 云端还是本地,数据放哪儿更安全之泄密事件类型分析篇