Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// 本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//1.创建一组动作
let userAction = UIMutableUserNotificationAction()
userAction.identifier = "action"
userAction.title = "Accept"
userAction.activationMode = UIUserNotificationActivationMode.Foreground
let userAction2 = UIMutableUserNotificationAction()
userAction2.identifier = "action2"
userAction2.title = "Ingore"
userAction2.activationMode = UIUserNotificationActivationMode.Background
userAction2.authenticationRequired = true
userAction2.destructive = true
//2.创建动作的类别集合
let userCategory = UIMutableUserNotificationCategory()
userCategory.identifier = "MyNotification"
userCategory.setActions([userAction,userAction2], forContext: UIUserNotificationActionContext.Minimal)
let categories:NSSet = NSSet(object: userCategory)
//3.创建UIUserNotificationSettings,并设置消息的显示类类型
let userSetting = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert]
, categories: categories as? Set<UIUserNotificationCategory>)
//4.注册推送
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(userSetting)
return true
}
// 当App既将进入后台、锁屏、有电话进来时会触发此事件
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
// 当App进入后台时触发此事件,此时在applicationDidEnterBackground方法内写入以下代码:
// 此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”
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()
let notification = UILocalNotification()
//notification.fireDate = NSDate().dateByAddingTimeInterval(1)
//setting timeZone as localTimeZone
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.Day
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)
}
/*
点击推送消息的按钮时会触发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) {
print("identifier=\(identifier)") //这里的identifier是按钮的identifier
completionHandler() //最后一定要调用这上方法
UIApplication.sharedApplication().cancelAllLocalNotifications()
let userInfo = notification.userInfo!
let title = userInfo["key"] as! String
let alert = UIAlertView()
alert.title = title
alert.message = notification.alertBody
alert.addButtonWithTitle(notification.alertAction!)
alert.cancelButtonIndex = 0
alert.show()
}
// 当App从后台即将回到前台时触发此事件
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
// 当App变成活动状态时触发此事件
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.
// 当程序处于活动状态的时候清除ICON的角标
application.cancelAllLocalNotifications()
application.applicationIconBadgeNumber = 0
}
// 当App退出时触发此方法,一般用于保存某些特定的数据
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件的更多相关文章
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...
- SWIFT推送之本地推送(UILocalNotification)
本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. 1.首 ...
- SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...
- IOS中程序如何进行推送消息(本地推送,远程推送)
[1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...
- ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送
iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...
- [iOS 高级] iOS远程推送与本地推送大致流程
本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...
- IOS中程序如何进行推送消息(本地推送,远程推送)2(上)
未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...
- IOS中程序如何进行推送消息(本地推送,远程推送)2(下)
内容中包含 base64string 图片造成字符过多,拒绝显示
随机推荐
- Git 常用命令2
Git 常用命令 Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ gi ...
- BZOJ1861——book
就是给你一摞书,然后又询问第k大,编号为x的书是第几大,然后修改书的位置 splay模板题...然而我还是不会,,,又遇到lrj的书的坑了,rj的书里没有father,顿时弄得我ask不会搞了 注意合 ...
- call(),apply()和bind()
三个函数都是Function对象自带的三个方法,主要作用是改变函数中this的指向. call() 语法 fun.call(thisArg[, arg1[, arg2[, ...]]]) 该方法可以传 ...
- c里面的fflush函数
NAME fflush - flush a stream SYNOPSIS #include <stdio.h> int fflush(FILE *stream); DESCRIPTION ...
- spring的对象属性相同(类型,名字)拷贝
A类: package test; /** * Created by gmq on 2015/12/4. */ public class A { private String aa; private ...
- 华为 MATE7 调试 LOCAT 日志不输出问题
[转]华为 MATE7 调试 LOCAT 日志不输出问题 http://www.cnblogs.com/glaivelee/p/4593221.html 用手机进行调试,在电脑上不显示logcat信息 ...
- python迭代器与iter()函数实例教程
python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...
- ACM/ICPC 之 并查集-食物链(POJ1182)
并查集的经典题型,POJ上题目还是中文= =,一般看到中文题都会感觉不太简单,这道题的数学归纳用得比较多,可以简化代码,挺有意思的. 同类型的题目还有POJ1703,比这个要简单,想了解并查集基本介绍 ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- ffmpeg-20160506-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...