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

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:.

}

}

转载于:https://www.cnblogs.com/gongyuhonglou/p/10311573.html

Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件相关推荐

  1. iOS开发 - ANPs推送通知 标签: 推送通知ANPs远程推送、本地推送

    iOS开发 - ANPs推送通知 标签: 推送通知ANPs远程推送本地推送 2015-05-03 14:12 3510人阅读 评论(0) 收藏 举报 本文章已收录于:  iOS知识库  分类: [IO ...

  2. SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息

    上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...

  3. Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送

    一.JPush PhoneGap  安装配置 官方安装API: GitHub - jpush/jpush-phonegap-plugin: JPush's officially supported P ...

  4. 本地推送通知和远程推送通知

    推送通知 推送通知跟NSNotification有所区别: 1> NSNotification是抽象的,不可见的 2> 推送通知是可见的(能用肉眼看到) iOS中提供了2种推送通知: 本地 ...

  5. iOS-本地推送和远程推送,常用的三方推送和常用的测试方法,推送实现和原理详解...

    什么是消息推送 举一个常见的例子,我们的手机上经常会有弹出一些信息,例如QQ信息.微信信息等等,这就是常见的消息推送. 例如: 消息推送的类型: 在屏幕顶部显示一块横幅(显示具体内容) 在屏幕中间弹出 ...

  6. iOS 推送通知及推送扩展

    概述 iOS中的通知包括本地推送通知和远程推送通知,两者在iOS系统中都可以通过弹出横幅的形式来提醒用户,点击横幅会打开应用.在iOS 10及之后版本的系统中,还支持通知扩展功能(UNNotifica ...

  7. Swift 本地推送通知UILocalNotification

    Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...

  8. iOS本地推送UILocalNotification

    本地通知主要是基于app本身定时器的行为.即使app在后台,也会发送本地通知.一个app只能有有限数量的预定通知,最多允许最近的64条通知,其余通知将会被系统忽略. 推送通知的呈现效果: 在屏幕顶部显 ...

  9. iOS10推送通知(本地远程)/Swift

    2019独角兽企业重金招聘Python工程师标准>>> iOS10本地通知 一.发送一个简单的本地通知 1.注册通知 需要导入头文件或者框架UserNotifications 在iO ...

最新文章

  1. 从0到1演示用 Git Rerere 自动解决冲突
  2. RNN-循环神经网络和LSTM_01基础
  3. Delegate学习笔记
  4. 【Tensorflow】tf.set_random_seed(seed)
  5. vue-router的hash模式和history模式,
  6. java clone方法_Java Calendar clone()方法与示例
  7. poj2393 其它贪心 挑战程序设计竞赛
  8. Bootstrap pc pad phone 响应式布局
  9. leetcode 179 python
  10. 嵌入式设计---(2)任务管理与调度
  11. 写一个function,清除字符串前后的空格。(兼容所有浏览器)
  12. 找到指针的奇数位置 c语言,(ppt)【C语言程序设计】上机作业2010.ppt
  13. 数据绑定(九)Binding的数据校验
  14. 最新伯乐PHP个人在线自动发卡网源码V3.1
  15. python批量修改图片大小_Python3 批量修改JPG图片尺寸?
  16. 如何处理电脑长时间未操作出现的假死?
  17. PR2018模板|手机竖屏图文视频制作剪辑素材/抖音,快手等短视频创作模板
  18. PS怎么旋转图片方向
  19. 微信小程序:未找到 app.json 中的定义的 pages “pages/index/index“ 对应的 WXML 文件
  20. 【2020-07】字节跳动面试凉经(年轻人的第一场 技术面试)

热门文章

  1. 不得不赞!一个国内(可能)最好的海量CV数据集获取网站
  2. CMU黑科技,手机录视频,实现人脸3D建模的高度逼真
  3. DeOccNet:国防科大提出阵列相机去除前景遮挡成像新方法
  4. Anime4K:目前最热的开源实时动漫放大算法,Github上一周收获2600星!
  5. 系统架构设计师面试java架构师 面试经验分享
  6. 带你自学Python系列(七):Python列表复制陷阱
  7. 屏蔽预训练模型的权重。 只训练最后一层的全连接的权重。_轻量化 | 如何让笨重的深度学习模型在移动设备上跑起来?看它!...
  8. 干部年龄大 计算机水平,各级别公务员“晋升年龄表”来了,超过这个年龄,以后基本上没戏...
  9. java方便适配器_Java适配器
  10. mysql 客户端_Linux桌面应用之MySQL客户端DBeaver