IOS本地推送即IOS备忘提醒实现
2016-05-21 16:53
423 查看
/**
* 解除某一个本地推送
*
* @param nameStr
本地推送的名字,需要自己起一个,可以用时间戳,同一时间不可能创建多个备忘。
*/
+(void)closeSomeLocalNotificationWithName:(NSString*)timestamp{
if (timestamp==nil) {
return;
}
//
获得 UIApplication
UIApplication *app = [UIApplication
sharedApplication];
//获取本地推送数组
NSArray *localArray = [app
scheduledLocalNotifications];
//如果存在本地推送数组,则遍历该数组找出符合条件的推送,关掉
if (localArray&&[localArray
count]>0) {
for (UILocalNotification *notification
in localArray)
{
NSDictionary *dict = notification.userInfo;
if ([timestamp
isEqualToString:ClearAllLocalNotification]) {
[app cancelLocalNotification:notification];
}
else if ([dict
isKindOfClass:[NSDictionary
class]]) {
NSString *inKey = [dict
objectForKey:@"name"];
if ([inKey
isEqualToString:timestamp]) {
[app cancelLocalNotification:notification];
break;
}
}
}
}
}
/**
* 添加一个本地推送
*
* @param name
推送名称,一般使用时间戳
* @param remind
本地推送发出的时间
* @param repeat
重复类型
*/
+(void)addLocalNotificationWithName:(NSString*)name
remind:(NSDate*)remind
repeat:(NSInteger)repeat
content:(NSString*)content
{
if([[NSDate
date] timeIntervalSinceDate:remind]>0 && repeat==0){
//已逾期且不重复提醒,则不注册本地推送
return;
}
UILocalNotification *notification=[[UILocalNotification
alloc]
init];
if (notification!=nil) {
// //设置通知的提醒时间
// NSDate* remindTime = [FunctionUnit convertDateFromString:remind Formate:TimeStrFormat];
notification.fireDate=remind;//多长时间后后通知
notification.timeZone=[NSTimeZone
defaultTimeZone];//使用本地时区
//
设置重复间隔
switch (repeat) {
case 0://不循环
notification.repeatInterval=0;//循环次数,kCFCalendarUnitWeekday一周一次
break;
case 1://每天循环
notification.repeatInterval=kCFCalendarUnitDay;
break;
case 2://每周循环
notification.repeatInterval=kCFCalendarUnitWeek;
break;
case 3://每月循环
notification.repeatInterval=kCFCalendarUnitMonth;
break;
case 4://每季循环
notification.repeatInterval=kCFCalendarUnitQuarter;
break;
case 5://每年循环
notification.repeatInterval=kCFCalendarUnitYear;
break;
default:
break;
}
//设置应用程序右上角的提醒个数
notification.applicationIconBadgeNumber+=1;
//声音,可以换成alarm.soundName = @"myMusic.caf"
notification.soundName=
UILocalNotificationDefaultSoundName;
//去掉下面2行就不会弹出提示框
//提示信息
弹出提示框
notification.alertBody=[NSString
stringWithFormat:@"您有备忘:%@需要处理",content];
//提示框按钮
notification.alertAction =
@"确定";
//是否显示额外的按钮,为no时alertAction消失
notification.hasAction =
NO;
//设置userinfo方便在之后需要撤销的时候使用,直接加一个时间戳好了
NSDictionary *infoDict = [NSDictionary
dictionaryWithObjectsAndKeys:name,@"name",@"memo",@"memo",
nil];
//添加额外的信息
notification.userInfo = infoDict;
//
将通知添加到系统中
[[UIApplication
sharedApplication] scheduleLocalNotification:notification];
}
}
* 解除某一个本地推送
*
* @param nameStr
本地推送的名字,需要自己起一个,可以用时间戳,同一时间不可能创建多个备忘。
*/
+(void)closeSomeLocalNotificationWithName:(NSString*)timestamp{
if (timestamp==nil) {
return;
}
//
获得 UIApplication
UIApplication *app = [UIApplication
sharedApplication];
//获取本地推送数组
NSArray *localArray = [app
scheduledLocalNotifications];
//如果存在本地推送数组,则遍历该数组找出符合条件的推送,关掉
if (localArray&&[localArray
count]>0) {
for (UILocalNotification *notification
in localArray)
{
NSDictionary *dict = notification.userInfo;
if ([timestamp
isEqualToString:ClearAllLocalNotification]) {
[app cancelLocalNotification:notification];
}
else if ([dict
isKindOfClass:[NSDictionary
class]]) {
NSString *inKey = [dict
objectForKey:@"name"];
if ([inKey
isEqualToString:timestamp]) {
[app cancelLocalNotification:notification];
break;
}
}
}
}
}
/**
* 添加一个本地推送
*
* @param name
推送名称,一般使用时间戳
* @param remind
本地推送发出的时间
* @param repeat
重复类型
*/
+(void)addLocalNotificationWithName:(NSString*)name
remind:(NSDate*)remind
repeat:(NSInteger)repeat
content:(NSString*)content
{
if([[NSDate
date] timeIntervalSinceDate:remind]>0 && repeat==0){
//已逾期且不重复提醒,则不注册本地推送
return;
}
UILocalNotification *notification=[[UILocalNotification
alloc]
init];
if (notification!=nil) {
// //设置通知的提醒时间
// NSDate* remindTime = [FunctionUnit convertDateFromString:remind Formate:TimeStrFormat];
notification.fireDate=remind;//多长时间后后通知
notification.timeZone=[NSTimeZone
defaultTimeZone];//使用本地时区
//
设置重复间隔
switch (repeat) {
case 0://不循环
notification.repeatInterval=0;//循环次数,kCFCalendarUnitWeekday一周一次
break;
case 1://每天循环
notification.repeatInterval=kCFCalendarUnitDay;
break;
case 2://每周循环
notification.repeatInterval=kCFCalendarUnitWeek;
break;
case 3://每月循环
notification.repeatInterval=kCFCalendarUnitMonth;
break;
case 4://每季循环
notification.repeatInterval=kCFCalendarUnitQuarter;
break;
case 5://每年循环
notification.repeatInterval=kCFCalendarUnitYear;
break;
default:
break;
}
//设置应用程序右上角的提醒个数
notification.applicationIconBadgeNumber+=1;
//声音,可以换成alarm.soundName = @"myMusic.caf"
notification.soundName=
UILocalNotificationDefaultSoundName;
//去掉下面2行就不会弹出提示框
//提示信息
弹出提示框
notification.alertBody=[NSString
stringWithFormat:@"您有备忘:%@需要处理",content];
//提示框按钮
notification.alertAction =
@"确定";
//是否显示额外的按钮,为no时alertAction消失
notification.hasAction =
NO;
//设置userinfo方便在之后需要撤销的时候使用,直接加一个时间戳好了
NSDictionary *infoDict = [NSDictionary
dictionaryWithObjectsAndKeys:name,@"name",@"memo",@"memo",
nil];
//添加额外的信息
notification.userInfo = infoDict;
//
将通知添加到系统中
[[UIApplication
sharedApplication] scheduleLocalNotification:notification];
}
}
相关文章推荐
- 总结---我在IOS开发中常用的回调手段
- iOS QQ实现第三方登录以及遇到的问题
- iOS设置启动图标
- IOS文件保存(重名不覆盖解决方案)
- iOS 更改全局字体
- iOS 去掉导航栏下面黑线 —— HERO博客
- iOS:shareSDK第三方登录
- iOS小技巧 - 设置TextField光标颜色
- iOS常见问题
- iOS - 通过runtime获取某个类中所有的变量和方法
- iOS基于MVC的项目重构总结
- iOS开发经验1.0
- iOS-block三种应用场景
- ios的hitTest方法以及不规则区域内触摸事件处理方法
- IOS 开发 事件截断处理
- IOS 开发 手势 及事件传递机理
- IOS 开发 手势使用
- IOS中 Block用法进阶二 局部变量和全局变量在Block中的使用
- IOS中 Block用法进阶一
- iOS static变量