您的位置:首页 > 产品设计 > UI/UE

UILocalNotification本地通知的使用方法

2016-01-12 13:56 399 查看
本文所写方法主要应用UILocalNotification达到本地推送通知栏信息

取消了其他教程里过期的UIAlertView方法

使用UILocalNotification主要分为创建 调用 取消 三个步骤

同时注意 如果调用[NSDate dateWithTimeIntervalSince1970:alertTime]这个方法 这个时间不是从显示1970年1月1日开始计算 而是1970年1月1日8点开始计算

具体详见格林威治时间相关信息

1.创建UILocalNotification 分别在AppDelegate和具体实现通知的Controller中写入以下代码 需要注意的是创建方法中的Key值 是用于后面取消时候的标记

并且同时要注意 每次添加新的通知 要把以前的通知去掉 否则以前的通知不会被新的通知覆盖 即两个通知会同时存在

AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//取消徽章
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

#pragma mark 本地通知回调函数 当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//更新显示的徽章个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
//取消通知推送
[MainViewController cancelLocalNotificationWithKey:@"weather"];
}


Controller

#pragma mark 本地通知功能
+ (void)registerLocalNotification:(NSInteger)alertTime {
//建立本地通知对象
UILocalNotification *notification=[[UILocalNotification alloc]init];
//设置触发通知的时间
NSDate *fireDate=[NSDate dateWithTimeIntervalSince1970:alertTime];
NSLog(@"触发通知的时间=%@",fireDate);
notification.fireDate=fireDate;
//设置时区
notification.timeZone=[NSTimeZone defaultTimeZone];
//设置重复的间隔
notification.repeatInterval=kCFCalendarUnitDay;
//设置通知内容
notification.alertBody=@"早安哦~今天也很想你";
notification.applicationIconBadgeNumber=1;
//通知被触发时播放的声音
notification.soundName=UILocalNotificationDefaultSoundName;
//创建本地通知的info信息 用于取消通知
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"weather"];
notification.userInfo = info;
//ios8后 需要添加这个注册 才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//通知重复提示的单位 可以是天 周 月
notification.repeatInterval = NSCalendarUnitDay;
} else {
//通知重复提示的单位 可以是天 周 月
notification.repeatInterval = NSDayCalendarUnit;
}
// 执行通知注册
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

#pragma mark 取消某个本地通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
//获取所有本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;

for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
//根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
//如果找到需要取消的通知,则取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}


2.调用UILocalNotification 因为上面代码把调用方法封装成了类方法 直接用相应的Controller类直接调用

#pragma mark 调用本地通知方法
- (void)localNotification
{
//调用本地通知方法
[MainViewController registerLocalNotification:p];
NSLog(@"开启本地通知");
}


3.取消UILocalNotification 取消方法同理 也是类方法的调用 根据定义时的方法中Key值取消相应的通知

-(void)notificationSwitch
{
if (noticeSwitch.on==YES) {
//调用本地通知
[self localNotification];
NSLog(@"开启本地通知");
}
if (noticeSwitch.on==NO) {
[MainViewController cancelLocalNotificationWithKey:@"weather"];
NSLog(@"关闭本地通知");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: