SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅:
继上一篇的内容进行小小的改动:
在didFinishLaunchingWithOptions方法内进行以下修改
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
// APService.registerForRemoteNotificationTypes(
// UIUserNotificationType.Badge.rawValue |
// UIUserNotificationType.Sound.rawValue |
// UIUserNotificationType.Alert.rawValue,
// categories: setting.categories) //1.创建一组动作
var userAction = UIMutableUserNotificationAction()
userAction.identifier = "action"
userAction.title = "Accept"
userAction.activationMode = UIUserNotificationActivationMode.Foreground var userAction2 = UIMutableUserNotificationAction()
userAction2.identifier = "action2"
userAction2.title = "Ingore"
userAction2.activationMode = UIUserNotificationActivationMode.Background
userAction2.authenticationRequired = true
userAction2.destructive = true //2.创建动作的类别集合
var userCategory = UIMutableUserNotificationCategory()
userCategory.identifier = "MyNotification"
userCategory.setActions([userAction,userAction2], forContext: UIUserNotificationActionContext.Minimal)
var categories:NSSet = NSSet(object: userCategory) //3.创建UIUserNotificationSettings,并设置消息的显示类类型
var userSetting = UIUserNotificationSettings(forTypes:
UIUserNotificationType.Badge |
UIUserNotificationType.Sound |
UIUserNotificationType.Alert
, categories: categories as Set<NSObject>) //4.注册推送
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(userSetting) }
2.修改applicationDidEnterBackground方法
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.category = "MyNotification" //这个很重要,跟上面的动作集合(UIMutableUserNotificationCategory)的identifier一样
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)
}
3.点击推送消息的按钮时会触发func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {}这个方法。
如果是远程推送那就是func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {}这个方法。
这里只需要调用本地第一个方法即可
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
println("identifier=\(identifier)") //这里的identifier是按钮的identifier completionHandler() //最后一定要调用这上方法
}
SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息的更多相关文章
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...
- SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. 1.首 ...
- ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送
iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...
- [iOS 高级] iOS远程推送与本地推送大致流程
本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...
- IOS 本地推送(UILocalNotification)
推送通知 ● 注意:这里说的推送通知跟NSNotification有所区别 • NSNotification是抽象的,不可见的 • 推送通知是可见的(能用肉眼看到) ● iOS中提供了2种推送通知 ● ...
- 包教包会:本地推送 & 远程推送
什么是推送?注意,和我们常用的抽象通知不同(NSNotification): 可以让不在前台运行的app,告知用户app内部发生了什么事情:或者没有运行的app接收到服务器发来的通知..比如离线QQ接 ...
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...
随机推荐
- asp.net <asp:Repeater>下的radio的单选使用
aspx页面 <asp:Repeater ID="rptData" runat="server"> <ItemTemplate> < ...
- Java东西太多,记录一些知识点
实习两个月了,这两个月接触了不少东西,简单列举一下知识,未来需要多多学习和了解. 1.前端js.extjs4.Jquery(js框架这些基本现学现用): 2.基础不好要补补Servlet和JSP(再往 ...
- SQL 常用的命令
--修改表名 --格式:SP_RENAME TABLENAME,NEWTABLENAME SP_RENAME TABLENAME,NEWTABLENAME --只能对表,不能对临时表 --修改字段名 ...
- 装B必备之 快捷键配置
作为一个程序员 所有程序都用快捷来这是装B必备的无形装B 最为致命.... 开始搞起 第一步配置环境变量 在系统D盘新建一个文件夹 D:\cache; 然后把这个路径 配置上 D:\cache; 最 ...
- spark必知必会的基本概念
首先我们从宏观的视角来窥视下大数据技术框架: 图1 大数据技术框架 从图1可以看出,数据源-数据收集-数据存储-资源管理,这是我们进行数据分析和处理的基本;图中的计算框架包括批处理.交互式分析和流处理 ...
- pycharm Django
上面的两张图片,是Django项目出错的图片,记得以前也出现过这个情况,当时好像是关闭了一些端口程序,后来就可以了,但是忘记了,那个链接也找不到了,所以现在很困惑,再找找. 电脑上现在程序安装的太多, ...
- angular惰性加载拓展剖析
最近把一个比较旧的业余项目重新升级了下,将主文件进行了剥离,增加了些惰性加载的配置,将过程中一些零散的知识点做个总结,同时尽量深入原理实现层面. 项目环境: 前端框架:angular2.0.0-bet ...
- [ORA-28001: the password has expired]的处理
http://irikintwtr.com/wordpress/?p=420 alter profile default limit password_life_time unlimited; alt ...
- ZendFramework中实现自动加载models
最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...
- 认识网页:html + css + JavaScript
参考资料:爬虫课程 认识网页 使用chrome,右键检查,查看网页源码,左侧的html,右侧的css,底下的JavaScript. 网页 = HTML(内容) + CSS(样式) + JavaScri ...