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)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...
随机推荐
- 机器学习-数据可视化神器matplotlib学习之路(一)
直接上代码吧,说明写在备注就好了,这次主要学习一下基本的画图方法和常用的图例图标等 from matplotlib import pyplot as plt import numpy as np #这 ...
- 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法
java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...
- MongoDB(课时8 模运算)
3.4.2.3 求模 模运算使用“$mod”来完成,语法: {$mod : [除数,余数]} 范例:求模 db.students.find({"age" : {"$mod ...
- [原][译][osg][osgEarth]飞行模拟软件JSBSim的操作(FGFCS类)
英文原文在 FGFCS.h头文件中 JSBSim的控制操作封装了飞行控制系统(FCS)的功能. 这个FGFCS类还封装了相同的“系统”和“自动驾驶仪”能力. FGFCS包含用来定义一个系统或飞行模型体 ...
- Codeforces 140D - New Year Contest
140D - New Year Contest 思路:贪心+排序.罚时与时间成正比,因为在0点前做完的题都可以在0点提交.从时间短的开始做最优. 代码: #include<bits/stdc++ ...
- C语言的的free和c++的delete的区别
首先free对应的是malloc:delete对应的是new:free用来释放malloc出来动态内存,delete用来释放new出来的动态内存空间. 应用的区别为: 1. 数组的时候int *p=( ...
- 新概念 Lesson 5 How are you today
How is Emma? 艾玛身体还好吗? 短语:very well How's Emma? She's very well, too. Emma is very well today adv. ...
- Filters: before, after, around, 常用到before_action,执行顺序是从外到内。
Filters是继承的,所以可以在ApplicationControlooer中设置filters. 如果有多个过滤,它们执行的顺序 先父类,然后往下推倒到子类 同一个类,按照代码上下顺序执行.
- JQuery.Ajax()的data参数传递方式
最近,新学c# mvc,通过ajax post方式传递数据到controller.刚开始传递参数,controller中总是为null.现记录一下,可能不全,纯粹记个学习日记. 重点在于参数的方式,代 ...
- JS冒泡排序的6种写法(武当雄风)
天下英雄出我辈,一入江湖岁月催.鸿图霸业谈笑间,不胜人生一场醉. 武当山上,一年一度的试道大会又开始了... 众武当弟子摩拳擦掌都想在此次试道大会上一展风采... 张三丰临终前曾留下一句话:试道大会采 ...