iOS10 添加本地推送(Local Notification)


转: http://blog.csdn.net/lincsdnnet/article/details/52970747


iOS10 添加本地推送(Local Notification)

新的推送注册机制

[objc]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import <UserNotifications/UserNotifications.h>  
  2. #import "AppDelegate.h"  
  3. @interface AppDelegate ()<UNUserNotificationCenterDelegate>  
  4.   
  5. @end  
  6.   
  7. @implementation AppDelegate  
  8.   
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  10.     // 使用 UNUserNotificationCenter 来管理通知  
  11.     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];  
  12.     //监听回调事件  
  13.     center.delegate = self;  
  14.       
  15.     //iOS 10 使用以下方法注册,才能得到授权  
  16.     [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)  
  17.                           completionHandler:^(BOOL granted, NSError * _Nullable error) {  
  18.                               // Enable or disable features based on authorization.  
  19.                           }];  
  20.       
  21.     //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取  
  22.     [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {  
  23.           
  24.     }];  
  25.     return YES;  
  26. }  
  27.   
  28. #pragma mark - UNUserNotificationCenterDelegate  
  29. //在展示通知前进行处理,即有机会在展示通知前再修改通知内容。  
  30. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{  
  31.     //1. 处理通知  
  32.       
  33.     //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式  
  34.     completionHandler(UNNotificationPresentationOptionAlert);  
  35. }  
  36. @end  

推送本地通知

[objc]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. //使用 UNNotification 本地通知  
  2. +(void)registerNotification:(NSInteger )alerTime{  
  3.       
  4.     // 使用 UNUserNotificationCenter 来管理通知  
  5.     UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];  
  6.       
  7.     //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。  
  8.     UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];  
  9.     content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];  
  10.     content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"  
  11.  arguments:nil];  
  12.     content.sound = [UNNotificationSound defaultSound];  
  13.       
  14.     // 在 alertTime 后推送本地推送  
  15.     UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger  
  16.  triggerWithTimeInterval:alerTime repeats:NO];  
  17.   
  18.     UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"  
  19.  content:content trigger:trigger];  
  20.       
  21.     //添加推送成功后的处理!  
  22.     [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {  
  23.         UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];  
  24.         UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];  
  25.         [alert addAction:cancelAction];  
  26.         [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];  
  27.     }];  
  28. }  

iOS 10 以前本地推送通知:

[objc]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. + (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {  
  2.     // ios8后,需要添加这个注册,才能得到授权  
  3.     // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
  4.     // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
  5.     // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
  6.     // categories:nil];  
  7.     // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
  8.     // // 通知重复提示的单位,可以是天、周、月  
  9.     // }  
  10.       
  11.     UILocalNotification *notification = [[UILocalNotification alloc] init];  
  12.     // 设置触发通知的时间  
  13.     NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];  
  14.     NSLog(@"fireDate=%@",fireDate);  
  15.       
  16.     notification.fireDate = fireDate;  
  17.     // 时区  
  18.     notification.timeZone = [NSTimeZone defaultTimeZone];  
  19.     // 设置重复的间隔  
  20.     notification.repeatInterval = kCFCalendarUnitSecond;  
  21.       
  22.     // 通知内容  
  23.     notification.alertBody =  @"该起床了...";  
  24.     notification.applicationIconBadgeNumber = 1;  
  25.     // 通知被触发时播放的声音  
  26.     notification.soundName = UILocalNotificationDefaultSoundName;  
  27.     // 通知参数  
  28.     NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];  
  29.     notification.userInfo = userDict;  
  30.       
  31.     // ios8后,需要添加这个注册,才能得到授权  
  32.     if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
  33.         UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
  34.         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
  35.                                                                                  categories:nil];  
  36.         [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
  37.         // 通知重复提示的单位,可以是天、周、月  
  38.         notification.repeatInterval = NSCalendarUnitDay;  
  39.     } else {  
  40.         // 通知重复提示的单位,可以是天、周、月  
  41.         notification.repeatInterval = NSDayCalendarUnit;  
  42.     }  
  43.       
  44.     // 执行通知注册  
  45.     [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
  46. }  


效果图






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值