Swift - 本地消息的推送通知(附样例)
使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
1,推送消息的发送


--- AppDelegate.swift ---
import UIKit @UIApplicationMain class AppDelegate : UIResponder , UIApplicationDelegate { var window: UIWindow ? func application(application: UIApplication , didFinishLaunchingWithOptions NSObject : AnyObject ]?) Bool { //开启通知 let settings UIUserNotificationSettings (forTypes: Alert , Badge , Sound ], categories: nil ) application.registerUserNotificationSettings(settings) return true } func applicationWillResignActive(application: UIApplication ) } func applicationDidEnterBackground(application: UIApplication ) } func applicationWillEnterForeground(application: UIApplication ) } func applicationDidBecomeActive(application: UIApplication ) } func applicationWillTerminate(application: UIApplication ) } } |
--- ViewController.swift ---
import UIKit class ViewController : UIViewController { override func viewDidLoad() super .viewDidLoad() //发送通知消息 scheduleNotification(12345); //清除所有本地推送 //UIApplication.sharedApplication().cancelAllLocalNotifications() } //发送通知消息 func scheduleNotification(itemID: Int ){ //如果已存在该通知消息,则先取消 cancelNotification(itemID) //创建UILocalNotification来进行本地消息通知 let localNotification UILocalNotification () //推送时间(设置为30秒以后) localNotification.fireDate NSDate (timeIntervalSinceNow: //时区 localNotification.timeZone NSTimeZone .defaultTimeZone() //推送内容 localNotification.alertBody "来自hangge.com的本地消息" //声音 localNotification.soundName UILocalNotificationDefaultSoundName //额外信息 localNotification.userInfo "ItemID" :itemID] UIApplication .sharedApplication().scheduleLocalNotification(localNotification) } //取消通知消息 func cancelNotification(itemID: Int ){ //通过itemID获取已有的消息推送,然后删除掉,以便重新判断 let existingNotification self .notificationForThisItem(itemID) as UILocalNotification ? if existingNotification nil { //如果existingNotification不为nil,就取消消息推送 UIApplication .sharedApplication().cancelLocalNotification(existingNotification!) } } //通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification func notificationForThisItem(itemID: Int )-> UILocalNotification ? let allNotifications UIApplication .sharedApplication().scheduledLocalNotifications for notification in allNotifications! let info as ! String : Int ] let number "ItemID" ] if number nil && return notification as UILocalNotification } } return nil } override func didReceiveMemoryWarning() super .didReceiveMemoryWarning() } } |
2,点击推送消息的响应
application(application: UIApplication, didReceiveLocalNotification
notification: UILocalNotification)代理方法。

func application(application: UIApplication , didReceiveLocalNotification UILocalNotification ) //设定Badge数目 UIApplication .sharedApplication().applicationIconBadgeNumber let info as ! String : Int ] let number "ItemID" ] let alertController UIAlertController (title: "本地通知" , message: "消息内容:\(notification.alertBody)用户数据:\(number)" , preferredStyle: UIAlertControllerStyle . Alert )
alertController.addAction(cancel);
self .window?.rootViewController!.presentViewController(alertController, animated: true , nil ) } |
Swift - 本地消息的推送通知(附样例)的更多相关文章
- 基于swoole+Redis的消息实时推送通知
swoole+Redis将实时数据的推送 一 实现功能 设计师订单如果设计师未抢单,超时(5分钟)设计订单时时给设计师派送, 设计师公众号中收到派单信息 设计发布者收到派单成功信息 环境 centos ...
- 移动 UX 设计:如何设计推送通知
这个问题你一定想过,在移动用户体验设计领域中,如何设计好一条简单的推送通知. 你注意过么,每天从不同的 App 上收到的大量的推送通知与提醒,这些通知里有多少你真的有兴趣? 每天,用户对各种没用的通知 ...
- 背水一战 Windows 10 (121) - 后台任务: 推送通知
[源码下载] 背水一战 Windows 10 (121) - 后台任务: 推送通知 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 推送通知 示例演示如何接收推送通知/WebA ...
- APNS推送通知消息负载内容和本地格式字符串
来源:http://hi.baidu.com/tangly888/blog/item/62948520121870559358074f.html 翻译苹果文档 地址: 翻译:tangly http: ...
- Swift 本地推送通知UILocalNotification
Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotifica ...
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- (七十三)iOS本地推送通知的实现
iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...
- iOS 通知、本地通知和推送通知有什么区别? APNS机制。
本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...
- iOS 本地推送通知
1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...
随机推荐
- ps做gif 登陆下拉菜单效果
作者这里仅介绍登录动画的制作思路和简单过程.一些细节的制作,如登录框,每一帧的图像等都需要自己根据参考图慢慢完成.最终效果 1.新建大小适当的文件,背景填充暗蓝色.首先设计一个底座,主要用图层样式来完 ...
- 微软Azure云主机测试报告
http://www.cnblogs.com/sennly/p/4135658.html 1. 测试目的 本次测试的目的在于对微软云主机做性能测试,评估其是否能够满足我们业务的需求. 2. 测试项目 ...
- URAL-1991 The battle near the swamp 水题
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1991 水题.. //STATUS:C++_AC_15MS_333KB #include ...
- jquery属性选择器中|value和^value的区别
jquery的属性选择中有两个比较混淆:一个是[attribute^value], 另一个是[attribute|value]. 先看解释: [attribute^value]:选取属性的值以valu ...
- 使用cocos2d-x 3.2下载图片资源小例子
cocos2d-x(ios)下载资源可以使用以下两种方式: 第一种使用libcurl下载图片 使用这种方法需要注意的是,我们需要引入libcurl.a这个库,同时配置对应的库目录和头文件目录具体方法是 ...
- Swift 可选值(Optional Values)介绍
Optional的定义 Optional也是Objective-C没有的数据类型,是苹果引入到Swift语言中的全新类型,它的特点就和它的名字一样:可以有值,也可以没有值,当它没有值时,就是nil.此 ...
- C++11lambda表达式
[C++11lambda表达式] mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行. 使用示例: #include <vector> #include ...
- sql的join用法
SQL join 用于把来自两个或多个表的行结合起来,sql join主要包括inner join. left join .right join .full outer join. 先介绍一下表里面的 ...
- hdoj 1404 Digital Deletions(博弈论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404 一看就是博弈论的题目,但并没有什么思路,看了题解,才明白 就是求六位数的SG函数,暴力一遍,打表 ...
- 关于表格中td自动换行做法
两个条件 一是给table中第一个tr(也就是表头)下的td或者th宽度,代码如下 而是让文字碰到边界自动换行 css属性 style="word-wrap:break-word;word- ...