前言

iOS 10 中废弃了 UILocalNotification ( UIKit Framework ) 这个类,采用了全新的 UserNotifications Framework 来推送通知,从此推送通知也有了自己的标签 UN (这待遇真是没别人了),以及对推送功能的一系列增强改进(两个 extension 和 界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点。

本文主要查看了 iOS 10 的相关文档,整理出了在 iOS 10 下的本地推送通知,由于都是代码,就不多做讲解,直接看代码及注释,有问题留言讨论哦。

新的推送注册机制

注册通知( Appdelegate.m ):

#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 //监听回调事件
 center.delegate = self;
 
 //iOS 10 使用以下方法注册,才能得到授权
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert   UNAuthorizationOptionSound)
       completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
       }];
 
 //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  
 }];
 return YES;
}

#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前进行处理,即有机会在展示通知前再修改通知内容。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 //1. 处理通知
 
 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
 completionHandler(UNNotificationPresentationOptionAlert);
}
@end

推送本地通知

//使用 UNNotification 本地通知
 (void)registerNotification:(NSInteger )alerTime{
 
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
 
 //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
 UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
 content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
 content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
 arguments:nil];
 content.sound = [UNNotificationSound defaultSound];
 
 // 在 alertTime 后推送本地推送
 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
 triggerWithTimeInterval:alerTime repeats:NO];

 UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
 content:content trigger:trigger];
 
 //添加推送成功后的处理!
 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  [alert addAction:cancelAction];
  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
 }];
}

iOS 10 以前本地推送通知:

  (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {
 // ios8后,需要添加这个注册,才能得到授权
 // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
 // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
 // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
 // categories:nil];
 // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 // // 通知重复提示的单位,可以是天、周、月
 // }
 
 UILocalNotification *notification = [[UILocalNotification alloc] init];
 // 设置触发通知的时间
 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
 NSLog(@"fireDate=%@",fireDate);
 
 notification.fireDate = fireDate;
 // 时区
 notification.timeZone = [NSTimeZone defaultTimeZone];
 // 设置重复的间隔
 notification.repeatInterval = kCFCalendarUnitSecond;
 
 // 通知内容
 notification.alertBody = @"该起床了...";
 notification.applicationIconBadgeNumber = 1;
 // 通知被触发时播放的声音
 notification.soundName = UILocalNotificationDefaultSoundName;
 // 通知参数
 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
 notification.userInfo = userDict;
 
 // 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];
}

本文已被整理到了《iOS推送教程》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

iOS10添加本地推送(Local Notification)实例的更多相关文章

  1. iOS中指纹识别常见问题汇总

    最近在公司做了一个app要使用指纹支付的功能,在实现过程中遇到各种坑,今天小编抽抗给大家总结把遇到问题汇总特此分享到脚本之家平台,需要的朋友参考下

  2. iOS10 适配以及Xcode8配置总结

    这篇文章主要介绍了iOS10 适配以及Xcode8配置总结的相关资料,本文通过图文并茂的形式给大家介绍,非常不错具有参考借鉴价值,需要的朋友可以参考下

  3. iOS10推送教程详解

    这篇文章主要为大家详细介绍了iOS10推送开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. iOS10通知框架UserNotification理解与应用

    在iOS10系统中,通知被整合进了UserNotification框架,除了使通知的处理脱离了UIApplication,通知功能的相关开发更加结构化与模块化外,还新增开放了许多更加灵活的开发接口,现在,开发者可以为通知定义UI末班,添加媒体附件,需要的朋友可以参考下

  5. iOS10添加本地推送(Local Notification)实例

    这篇文章主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  6. 针对iOS10新增Api的详细研究

    这篇文章主要针对iOS10新增Api进行详细研究,基于Api层面,着重看一些具体用法所做的笔记,感兴趣的小伙伴们可以参考一下

  7. 详解适配iOS10 的相关权限设置

    在最新版本的iOS10系统中,如果你的项目中访问了隐私数据,比如:相机、相册、录音、定位、联系人等等。涉及到权限问题,本篇文章主要介绍了适配iOS10 的相关权限设置,有兴趣的可以了解一下。

  8. 兼容iOS 10 升级xcode8出现的问题及一些适配问题的解决方案

    这篇文章主要介绍了兼容iOS 10 升级xcode8出现的问题及一些适配问题的解决方案,非常不错,具有参考借鉴价值,需要的朋友可以参考下

  9. Xcode8、iOS10升级问题记录

    本文给大家分享xcode8,ios10升级后的问题记录,可以帮大家到家更好的解决xcode,ios10升级遇到问题,感兴趣的朋友一起看看吧

  10. iOS10适配以及Xcode8使用需要注意的那些坑

    这篇文章主要为大家详细介绍了iOS10的适配以及Xcode8使用需要注意的那些坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部