上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅:

继上一篇的内容进行小小的改动:

在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)之二带按钮的消息的更多相关文章

  1. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  2. Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件

    上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...

  3. Swift - 推送之本地推送(UILocalNotification)

    // 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...

  4. SWIFT推送之本地推送(UILocalNotification)

    本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. 1.首 ...

  5. ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送

    iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...

  6. [iOS 高级] iOS远程推送与本地推送大致流程

    本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...

  7. IOS 本地推送(UILocalNotification)

    推送通知 ● 注意:这里说的推送通知跟NSNotification有所区别 • NSNotification是抽象的,不可见的 • 推送通知是可见的(能用肉眼看到) ● iOS中提供了2种推送通知 ● ...

  8. 包教包会:本地推送 & 远程推送

    什么是推送?注意,和我们常用的抽象通知不同(NSNotification): 可以让不在前台运行的app,告知用户app内部发生了什么事情:或者没有运行的app接收到服务器发来的通知..比如离线QQ接 ...

  9. iOS 10 消息推送(UserNotifications)秘籍总结(二)

    背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...

随机推荐

  1. Python的替换函数——replace(),strip(),和re.sub()

    在Python中常用的三个"替换"函数是strip(),replace()和re.sub(),下面来讲讲这三个函数的用法. 一.replace() 基本用法:对象.replace( ...

  2. 微信小程序之可滚动视图 scroll-view 的使用注意

    微信小程序之可滚动视图 scroll-view 使用注意: 1.scroll-view 中的需要滑动的元素不可以用 float 浮动: 2.scroll-view 中的包裹需要滑动的元素的大盒子用 d ...

  3. Linux——用户管理简单学习笔记(四)

    主要讲两个用户管理的案例: 1: 限制用户su为root,只允许某个组的的用户su # groupadd sugroup 首先添加我们的用户组 # chmod 4550 /bin/su 改变命令的权限 ...

  4. Java并发编程:Callable、Future和FutureTask(转)

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  5. STL_算法_03_拷贝和替换算法

    ◆ 常用的拷贝和替换算法: 1.1.复制(容器A(全部/部分) 复制到 容器B(全部/部分)),返回的值==>iteratorOutBegin.end() iterator copy(itera ...

  6. Codeforces 680D - Bear and Tower of Cubes

    680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...

  7. HTTP协议的请求与响应和CSS属性和定位

    HTTP协议的请求与响应和CSS属性和定位 一.HTTP协议 1.1 HTTP定义 HTTP(Hypertext Transport Protocol),超文本传输协议. 一种详细规定了浏览器和web ...

  8. [转]mysql日常工作手记

    1. 给navy加show权限: 1 2 update mysql.user set Show_db_priv='Y'  where user='navy'; flush privileges; 2. ...

  9. English trip -- Review Unit3 Family 家人

    Words daughter grandfather grandmother husband wife uncle aunt brother sister Who is ...? Loki's ... ...

  10. Kali Linux on Android # 实测:小米2s离线安装Kali Linux

    小米2s 离线安装Kali Linux 2017年2月4日  by 小甘丶 前段时间也研究过一两天,没弄明白,今天突然来兴致了,说研究一下吧,结果一不小心,就弄明白了! 第一次研究,主要是没弄明白这个 ...