[转载]iOS本地推送-备用
第一步:创建本地推送
// 创建一个本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//设置10秒之后
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
// 设置推送时间
notification.fireDate = pushDate;
// 设置时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
// 推送声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容
notification.alertBody = @"推送内容";
//显示在icon上的红色圈中的数子
notification.applicationIconBadgeNumber = 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
notification.userInfo = info;
//添加推送到UIApplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification];
}
第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
// 图标上的数字减1
application.applicationIconBadgeNumber -= 1;
}
第三步:解除本地推送
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"对应的key值"]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//判断是否找到已经存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}
最新文章
- C#按位操作,直接操作INT数据类型的某一位
- Linux修改SSH端口,并禁止Root远程登陆
- ADO.NET学习系列(一)
- ios7.1 在线安装 失败的解决办法
- tuple只有一个元素的时候,必须要加逗号
- HTML+CSS+JS简介
- win10 mysql详尽安装教程
- 移动端的silder,未封装,基于zepto的touch模块,有参照修改过touch的bug
- pypi镜像源加速第三方库在线安装
- c++ CreateProcess调用dos命令
- [CocoaPods]使用Gemfile
- P2799国王的魔镜
- 使用 IntraWeb (15) - 基本控件之 TIWEdit、TIWMemo、TIWText
- 有序数组每个数平方后,不同数字的个数?O(n)
- spring--mvc用戶注册用户名验重
- PHP7 学习笔记(二)PHP5.9 升级到PHP7 遇到的一些坑的记录(php-fpm 图解)
- js解析与序列化json数据(一)json.stringify()的基本用法
- vue 时间戳转 YYYY-MM-DD h:m:s
- loj #6261 一个人的高三楼 FFT + 组合数递推
- bzoj 1043 下落的圆盘 —— 求圆心角、圆周长
热门文章
- Linux系统内核制作和内核模块的基础
- C#入门教程(一)–.Net平台技术介绍、C#语言及开发工具介绍-打造C#学习教程
- 简单遗传算法求解n皇后问题
- 关于Class.forName(";oracle.jdbc.driver.OracleDriver";);报ClassNotFoundException 的异常
- Java Concurrency - 浅析 CountDownLatch 的用法
- Commons JXPath - Extension Functions
- Spring(3.2.3) - Beans(6): 作用域
- 找出文件正在被哪个windows进程使用的方法
- sql常识-FULL JOIN
- iOS - 基于蓝牙数据交换的环境监测(温度、湿度、光照、粉尘、噪声)