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

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

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

在didFinishLaunchingWithOptions方法内进行以下修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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) {}这个方法。

这里只需要调用本地第一个方法即可

 

1
2
3
4
5
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        println("identifier=\(identifier)"//这里的identifier是按钮的identifier
         
        completionHandler()  //最后一定要调用这上方法
    }

转载于:https://www.cnblogs.com/Free-Thinker/p/6478650.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417237.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

树状数组基础

关于树状数组的讲解推荐《算法竞赛入门经典训练指南》 一维版本&#xff1a; 洛谷3374 分析&#xff1a;树状数组裸的模板题 1 #include<iostream>2 #include<cstdio>3 #include<cstring>4 using namespace std;5 const int maxn50000010;6 int c[maxn];7 in…

vue1升级到vue2的问题

router 不能用map方法了&#xff0c;需要改router的结构改为routers [ { // 当没有匹配路由时默认返回的首页 path:/index, component: index, authenticate:true }, { // 当没有匹配路由时默认返回的首页 path: /spa/, component: i…

Spring Boot 学习笔记--整合Thymeleaf

1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency> 2.添加静态文件 根据…

BZOJ 4241 分块

思路&#xff1a; 考虑分块 f[i][j]表示从第i块开头到j的最大值 cnt[i][j]表示从第i块开始到序列末尾j出现了多少次 边角余料处理一下就好啦~ //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> using namespace std; const int …

django ORM创建数据库方法

1、指定连接pymysql(python3.x) 先配置_init_.py import pymysqlpymysql.install_as_MySQLdb() 2、配置连接mysql文件信息 settings.py DATABASES {default: {ENGINE: django.db.backends.mysql, NAME: django_orm, #你的数据库名称USER: root, #你的数据库用户名PASSWOR…

【计算机视觉】基于OpenCV的人脸识别

一点背景知识 OpenCV 是一个开源的计算机视觉和机器学习库。它包含成千上万优化过的算法&#xff0c;为各种计算机视觉应用提供了一个通用工具包。根据这个项目的关于页面&#xff0c;OpenCV 已被广泛运用在各种项目上&#xff0c;从谷歌街景的图片拼接&#xff0c;到交互艺术展…

四则运算题目生成程序(基于控制台)

基于控制台的四则运算 代码地址 a.需求分析 运算符为 , −, , 除了整数以外&#xff0c;还要支持真分数的四则运算&#xff0c;真分数的运算&#xff0c;例如&#xff1a;1/6 1/8 7/24要求能处理用户输入的真分数&#xff0c; 如 1/2, 5/12 等并且要求能处理用户的输入&#…

前端学习(2306):react之组件使用

Home.js import React, {Component} from react;class Home extends Component {render() {return (<div><div>你好我是组件{parseInt(Math.random()*10)}</div><div>你好我是组件2{parseInt(Math.random()*10)}</div></div>);} }export …

前端学习(2306):react之组件使用之图片使用

Home.js import React, {Component,Fragment} from react; import ImgA from "../assset/index.jpg" class Home extends Component {render() {return (<Fragment><div>你好我是组件{parseInt(Math.random()*10)}</div><div>你好我是组件2…

php背景图添加字,怎样给视频后面加背景图 视频加背景图片并添加一行广告文字...

有不少广告小视频中&#xff0c;视频画面是一张海报背景图片&#xff0c;图片上显示一个小视频播放&#xff0c;并且在画面上还有显示一行广告字幕。这样的宣传视频制作其实蛮简单的&#xff0c;怎样给视频后面加背景图片的方法倒是挺多&#xff0c;要给视频加背景图片的同时还…

前端学习(2307):react之props和state

Home,js import React, {Component} from react; import News from "./News"; class Home extends Component {render() {return (<div>home<News name"你好"/></div>);} }export default Home; news,js import React, {Component} f…

前端学习(2308):react之子传父

Home,js import React, {Component} from react; import News from "./News"; class Home extends Component {constructor(props) {super(props);this.state{text:我是默认值}}dataFun(text)>{console.log(text)this.setState({text})}render() {return (<di…

linux 密码复杂度,用PAM 搞定Linux 平台密码复杂度问题

用PAM 搞定Linux 平台密码复杂度问题星期五, 十二月 27, 20130作为一个PAM的一个模块&#xff0c;pam_cracklib可以被用来检查密码是否违反密码字典&#xff0c;这个验证模块可以通过插入password堆栈&#xff0c;为特殊的应用提供可插入式密码强度性检测&#xff0c;能够很好地…

前端学习(2309):react之同级传值

Home,js import React, {Component} from react; import News from "./News"; class Home extends Component {constructor(props) {super(props);this.state{text:我是默认值}}dataFun(text)>{console.log(text)this.setState({text})}render() {return (<di…

前端学习(2310):数据请求和json-server

app.js import React from react;import ./App.css; import Home from ./components/Home.js import World from "./components/World"; function App() {return (<div className"App">你好<World/></div>); }export default App;worl…

对Linux课程内容的建议,Linux课程笔记 Day01 课程内容总结(示例代码)

系统安装&#xff1a;引导项简单介绍&#xff1a;在“boot:”提示后&#xff1a;直接回车(Enter)——图形界面安装模式linux text——字符界面安装模式linux askmethod——提示用户选择安装方法(例如&#xff1a;nfs、ftp、http远程安装)linux rescue——救援模式&#xff0c;…

前端学习(2311):react中处理跨域问题

proxy:{"/api":{target:"http://www.weather.com.cn/data/cityinfo/101320101.html",changeOrigin:true,"pathRewrite":{"^/api":"/"}}}

Dev Express Report 学习总结(五)在分组中使用聚集表达式AggregateExpression

聚集表达式AggregateExpression主要包括几种&#xff1a;Avg(),Count(),Exists(),Max(),Min(),Single()和Sum()。其中对于Sum()&#xff0c;在我看来主要有两种用法&#xff0c;一种是Group时的合计&#xff0c;另一种是整个页面某个列的值的合计。但是对于Count(),由于以前对D…