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,一经查实,立即删除!

相关文章

java的input不能更改,无法将方法响应标头Content-Type更改为application / xml

尝试解决Twilio 12300错误&#xff1a;使用AWS的API网关(GET)和CloudFormation通过 aws cli 和Swagger部署Lambda(无服务器)应用程序时导致的内容类型无效 .我将集成响应主体映射模板> Content-Type设置为 application/xml 和模板#set($inputRoot $input.path(S))$inputRoo…

.Net 调用wordCOM组件转PDF

运行环境&#xff1a;office2010或office2007SaveAsPDFandXPS.exe补丁 x86x64系统环境均可。class Program{static void Main(string[] args){DOCConvertToPDF(AppDomain.CurrentDomain.BaseDirectory "/b.html", AppDomain.CurrentDomain.BaseDirectory "/aa…

树状数组基础

关于树状数组的讲解推荐《算法竞赛入门经典训练指南》 一维版本&#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…

php pdo更新,php - 使用PDO和MySQL更新查询

我试图只用PDO编写更新查询&#xff0c;但我无法执行代码&#xff1f;try {$conn new PDO("mysql:host$hostdb; dbname$namedb", $userdb, $passdb);$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8$conn->setAttribute(PDO::ATTR_…

jquery.easyui常用示例

消息框$.messager.alert("提示","法规重复标志更新成功&#xff01;");$.messager.confirm(Confirm,Are you sure you want to delete record?,function(r){ if (r){ alert(ok); } }); 翻页//初始化$(#pagination).pagination({total: actResult.Ro…

vue1升级到vue2的问题

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

python寻找字符串中的英文字符,python如何解析字符串中出现的英文人名?

这里有四个例子&#xff0c;结果来自google scholarstr1 "Jakes, William C., and Donald C. Cox. Microwave mobile communications. Wiley-IEEE Press, 1994."str2 "Schlegel, David J., Douglas P. Finkbeiner, and Marc Davis. \"Maps of dust infra…

win2003/XP删除桌面回收站

gpedit.msc 用户配置——管理模板——桌面——从桌面删除回收站——启用刷新桌面即可。

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.添加静态文件 根据…

mongo php update 写法,mongodb update 字符 操作

下面常用的update操作&#xff0c;用mongodb2.6.3版本测试的&#xff0c;官方发布的稳定版本2.4&#xff0c;建议用稳定版。 一&#xff0c;upsert表示如果有数据就不插入&#xff0c;没数据就插入 1&#xff0c;命令行下 db.peoples.update( //查找name等于tank的用户... { na…

js 打印

window.print(); 打印隐藏部分<style type"text/css" media"print">.noprint{display:none;}</style>

oracle 增加一个新分区,oracle 11g 新增分区

oracle 11g新增了间隔分区、虚拟列分区和引用分区。详细的介绍请查看官方文件&#xff1a;1. 间隔分区间隔分区是范围分区的一种扩展。在引入间隔分区之前&#xff0c;DBA 需要显式定义每个分区的值范围&#xff0c;随着分区值的增长可用分区会逐渐减少直到没有可使用的分区为止…

jquery.uploadify flash IE6上传无效

类似$().window&#xff0c;以div方式打开弹窗&#xff0c;弹窗中iframe中就是上传页面&#xff0c;页面加载时就加载上传页面会产生这个问题。解决办法&#xff1a;打开弹窗后再给iframe赋src值。

如何手动修改oracle表空间,ORACLE数据库创建和修改表空间

-建立表空间(oracle中的tablespace(表空间))CREATE TABLESPACE data01DATAFILE D:\oracle\ora92\oradata\db\DATA01.dbf SIZE 200MUNIFORM SIZE 128k;#指定区尺寸为128k,如不指定&#xff0c;区尺寸默认为64k--建立临时表空间CREATE TEMPORARY TABLESPACE temp_dataTEMPFILE D:…

dexpler的使用方法

https://www.abartel.net/dexpler/转载于:https://www.cnblogs.com/yuqt/p/6490848.html

jquery.uploadify参数

uploadify函数的参数为json格式&#xff0c;可以对json对象的key值的修改来进行自定义的设置&#xff0c;如multi设置为true或false来控制是否可以进行多文件上传&#xff0c;下面就来介绍下这些key值的意思&#xff1a; uploader &#xff1a; uploadify.swf 文件的相对路径&a…

Kaggle入门篇

Kaggle入门篇转载于:https://www.cnblogs.com/akrusher/articles/6492096.html

超链接js点击后页面向上滚动问题解决

超链接js点击后页面向上滚动问题解决 <a href"#" οnclick"fun();return false;"></a>

Oracle数据导入要多久,oracle数据库备份导入要注意的几个问题

oracle数据库备份导入要注意的几个问题(1)oracle数据库备份的导入对数据库的版本有要求&#xff0c;也即源数据库(导出产生备份的数据库)的版本要和目标数据库(导入数据库)的版本一致&#xff0c;否则可能导致导入失败。(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 …