iOS开发 iOS10推送必看

iOS10更新以后,推送也是作了一些小小的修改,下面我就给你们仔细说说。但愿看完个人这篇文章,对你们有所帮助。php

1、简单入门篇---看完就能够简单适配完了

相对简单的推送证书以及环境的问题,我就不在这里讲啦,我在这里说的,是指原有工程的适配。数组

1.首先咱们须要打开下面的开关。全部的推送平台,不论是极光仍是什么的,要想收到推送,这个是必须打开的哟~app


QQ20160914-4.png

以后,系统会生成一个咱们之前没见过的文件,如图:框架


QQ20160918-0.png-5.8kB

QQ20160918-1.png-26.3kB

可能产生的问题:以前有朋友反馈过,将开发环境由 development 变成 production ,在开关这里会产生错误,如图:ide


QQ20160918-2.png-26.4kB

若是你们点击Fix issue以后,会惊奇的发现,APS Environment由 production 又变成 development 了。函数

解决办法:个人建议是不作任何修改。

通过个人测试,打包以后,生成的ipa包内,是没有这个.entitlements 文件的。通过测试,我发现是能够正常收到推送信息的。测试的方法以下,你们也能够测试一下。测试

测试方法:打包以后安装ipa文件,而后利用极光推送,选择生产环境,推送,便可。

通过上面的操做,你就会惊奇的发现,推送已经适配完毕了,iOS10的系统,已经能够正常接收通知了。fetch

2、中级篇

这里我会给你们讲一讲iOS10的推送,如何注册,经过什么代理,哪些方法能够用,哪些方法不能够用。ui

1.系统自带方法

你们不论是使用三方平台的推送,仍是系统自带的推送,都先应该了解下系统自带方法,如何实现远程通知的实现。spa

  • 第一步导入#import <UserNotifications/UserNotifications.h>
    且要遵照<UNUserNotificationCenterDelegate>的协议,在Appdelegate.m中。
    这里须要注意,咱们最好写成这种形式
    #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
    • 第二步咱们须要在 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中注册通知,代码以下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { //iOS10特有 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 必须写代理,否则没法监听通知的接收与点击 center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // 点击容许 NSLog(@"注册成功"); [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"%@", settings); }]; } else { // 点击不容许 NSLog(@"注册失败"); } }]; }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){ //iOS8 - iOS10 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]]; }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) { //iOS8系统如下 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } // 注册得到device Token [[UIApplication sharedApplication] registerForRemoteNotifications];

其中,得到Device Token的方法是没有改变的。

// 得到Device Token - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]); } // 得到Device Token失败 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }

这次iOS10系统的更新,苹果给了咱们2个代理方法来处理通知的接收和点击事件,这两个方法在<UNUserNotificationCenterDelegate>的协议中,你们能够查看下。此外,苹果把本地通知跟远程通知合二为一。区分本地通知跟远程通知的类是UNPushNotificationTrigger.h类中,UNPushNotificationTrigger的类型是新增长的,经过它,咱们能够获得一些通知的触发条件,在使用时,咱们不该该直接使用这个类,应当使用它的子类。

  • 我简单点说
  • 1.UNPushNotificationTrigger (远程通知) 远程推送的通知类型

  • 2.UNTimeIntervalNotificationTrigger (本地通知) 必定时间以后,重复或者不重复推送通知。咱们能够设置timeInterval(时间间隔)和repeats(是否重复)。

  • 3.UNCalendarNotificationTrigger(本地通知) 必定日期以后,重复或者不重复推送通知 例如,你天天8点推送一个通知,只要dateComponents为8,若是你想天天8点都推送这个通知,只要repeats为YES就能够了。

  • 4.UNLocationNotificationTrigger (本地通知)地理位置的一种通知,
    当用户进入或离开一个地理区域来通知。在CLRegion标识符必须是惟一的。由于若是相同的标识符来标识不一样区域的UNNotificationRequests,会致使不肯定的行为。

接收通知的代码以下:

// iOS 10收到通知 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ NSDictionary * userInfo = notification.request.content.userInfo; UNNotificationRequest *request = notification.request; // 收到推送的请求 UNNotificationContent *content = request.content; // 收到推送的消息内容 NSNumber *badge = content.badge; // 推送消息的角标 NSString *body = content.body; // 推送消息体 UNNotificationSound *sound = content.sound; // 推送消息的声音 NSString *subtitle = content.subtitle; // 推送消息的副标题 NSString *title = content.title; // 推送消息的标题 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]); } else { // 判断为本地通知 NSLog(@"iOS10 前台收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); } completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 须要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型能够设置 }

下面的代码则是通知的点击事件:

// 通知的点击事件 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ NSDictionary * userInfo = response.notification.request.content.userInfo; UNNotificationRequest *request = response.notification.request; // 收到推送的请求 UNNotificationContent *content = request.content; // 收到推送的消息内容 NSNumber *badge = content.badge; // 推送消息的角标 NSString *body = content.body; // 推送消息体 UNNotificationSound *sound = content.sound; // 推送消息的声音 NSString *subtitle = content.subtitle; // 推送消息的副标题 NSString *title = content.title; // 推送消息的标题 if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]); } else { // 判断为本地通知 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); } // Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called. completionHandler(); // 系统要求执行这个方法 }

在点击事件中,若是咱们不写completionHandler()这个方法,可能会报一下的错误,但愿你们注意下~
Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

最后最后,咱们要你们补充一下,旧版本的一些方法,方便你们扩充iOS10的通知的通知,不影响原有逻辑。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"iOS6及如下系统,收到通知:%@", [self logDic:userInfo]); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]); completionHandler(UIBackgroundFetchResultNewData); }

2.极光推送(须要下载最新的版本)

若是用到三方的一些平台,作推送就会更为简单。

1.注册通知的代码以下

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { #ifdef NSFoundationVersionNumber_iOS_9_x_Max JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; #endif } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //能够添加自定义categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { //categories 必须为nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; }

注册完成以后,咱们则须要加入极光推送更新后,新加入的2个方法,这两个方法在<JPUSHRegisterDelegate>代理方法中。

/* * @brief handle UserNotifications.framework [willPresentNotification:withCompletionHandler:] * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心 * @param notification 前台获得的的通知对象 * @param completionHandler 该callback中的options 请使用UNNotificationPresentationOptions */ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler; /* * @brief handle UserNotifications.framework [didReceiveNotificationResponse:withCompletionHandler:] * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心 * @param response 通知响应对象 * @param completionHandler */ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;

使用时,只须要在上面的代码中添加极光的处理方法就能够了,具体使用以下图:

if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { // 这个方法,不论是收到通知代理仍是点击通知的代理,若是使用极光推送,咱们都是须要增长这个方法的。 [JPUSHService handleRemoteNotification:userInfo]; NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]); [rootViewController addNotificationCount]; } else { // 判断为本地通知 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); }

经过上面的文章,相信你们已经能够初步了解新版本的推送,要如何处理啦~

稍后,我会更新如下的内容,但愿你们支持:
UNNotificationContentExtension - 通知内容扩展
UNNotificationServiceExtension- 通知服务扩展
UNNotificationAction - 通知响应的方法
UNErrorCode - 通知错误
UNNotificationAttachment - 附件通知
稍后我还会上传demo路径。

若是你喜欢个人文章,不要忘记关注我,谢谢你们了~
另外若是你要转载,但愿能够注明出处,我会写出更多更好的文章,来回馈你们~



 

 

 

 

虽然这篇文章比较长,也很差理解,可是仍是建议你们收藏,之后用到的时候,能够看看,有耐心的仍是读一读。

这篇文章开始,我会跟你们好好讲讲,苹果新发布的iOS10的全部通知类。

1、建立本地通知事例详解:

注意啊,小伙伴们,本地通知也必须在appdelegate中注册中心,通知的开关打不打开无所谓的,毕竟是本地通知,可是通知的接收的代理,以及通知点击的代理,苹果给合二为一了。因此你们仍是须要在appdelegate中写上这2个方法,还有不要忘记在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions注册通知中心。若是使用极光推送的小伙伴,写看一下个人基础篇,辛苦你们啦

建立一个UNNotificationRequest类的实例,必定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是能够用来区分这个通知与其余通知
把request加到UNUserNotificationCenter, 并设置触发器,等待触发
若是另外一个request具备和以前request相同的标识,不一样的内容, 能够达到更新通知的目的

建立一个本地通知咱们应该先建立一个UNNotificationRequest类,而且将这个类添加到UNUserNotificationCenter才能够。代码以下:

// 1.建立一个UNNotificationRequest NSString *requestIdentifer = @"TestRequest"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger]; // 2.将UNNotificationRequest类,添加进当前通知中心中 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];

在建立UNNotificationRequest类时,官方的解释是说,一个通知请求能够在预约经过时间和位置,来通知用户。触发的方式见UNNotificationTrigger的相关说明。调用该方法,在通知触发的时候。会取代具备相同标识符的通知请求,此外,消息个数受系统限制。

上面的翻译,看上去可能有些拗口,简单来讲,就是咱们须要为UNNotificationRequest设置一个标识符,经过标识符,咱们能够对该通知进行添加,删除,更新等操做。

如下是完整的建立通知的代码:

// 1.建立通知内容 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"徐不一样测试通知"; content.subtitle = @"测试通知"; content.body = @"来自徐不一样的简书"; content.badge = @1; NSError *error = nil; NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_certification_status1@2x" ofType:@"png"]; // 2.设置通知附件内容 UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error]; if (error) { NSLog(@"attachment error %@", error); } content.attachments = @[att]; content.launchImageName = @"icon_certification_status1@2x"; // 2.设置声音 UNNotificationSound *sound = [UNNotificationSound defaultSound]; content.sound = sound; // 3.触发模式 UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; // 4.设置UNNotificationRequest NSString *requestIdentifer = @"TestRequest"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger1]; //5.把通知加到UNUserNotificationCenter, 到指定触发点会被触发 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];

经过以上代码,咱们就能够建立一个5秒触发本地通知,具体样式能够看下图


QQ20160919-0@2x.png-173.2kB

下拉放大content.launchImageName = @"icon_certification_status1@2x";显示的图片是这行代码的效果,如图


IMG_0123.PNG

根据上面内容,你们会发如今建立UNNotificationRequest的时候,会须要UNMutableNotificationContent以及UNTimeIntervalNotificationTrigger这两个类。下面我就对相关的类,以及类扩展,作相应的说明。

1.UNNotificationContent以及UNMutableNotificationContent(通知内容和可变通知内容)

通知内容分为可变的以及不可变的两种类型,相似于可变数组跟不可变数组。后续咱们经过某一特定标识符更新通知,即是用可变通知了。
不论是可变通知仍是不可变通知,都有如下的几个属性:

// 1.附件数组,存放UNNotificationAttachment类 @property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments ; // 2.应用程序角标,0或者不传,意味着角标消失 @property (NS_NONATOMIC_IOSONLY, copy, nullable) NSNumber *badge; // 3.主体内容 @property (NS_NONATOMIC_IOSONLY, copy) NSString *body ; // 4.app通知下拉预览时候展现的图 @property (NS_NONATOMIC_IOSONLY, copy) NSString *launchImageName; // 5.UNNotificationSound类,能够设置默认声音,或者指定名称的声音 @property (NS_NONATOMIC_IOSONLY, copy, nullable) UNNotificationSound *sound ; // 6.推送内容的子标题 @property (NS_NONATOMIC_IOSONLY, copy) NSString *subtitle ; // 7.通知线程的标识 @property (NS_NONATOMIC_IOSONLY, copy) NSString *threadIdentifier; // 8.推送内容的标题 @property (NS_NONATOMIC_IOSONLY, copy) NSString *title ; // 9.远程通知推送内容 @property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *userInfo; // 10.category标识 @property (NS_NONATOMIC_IOSONLY, copy) NSString *categoryIdentifier;

以上的的属性,我都增长了相应的说明,你们能够对照个人注释来使用。

2.UNNotificationAttachment (附件内容通知)

在UNNotificationContent类中,有个附件数组的属性,这就是包含UNNotificationAttachment类的数组了。

@property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments ;

苹果的解释说,UNNotificationAttachment(附件通知)是指能够包含音频,图像或视频内容,而且能够将其内容显示出来的通知。使用本地通知时,能够在通知建立时,将附件加入便可。对于远程通知,则必须实现使用UNNotificationServiceExtension类通知服务扩展。

建立附件的方法是attachmentWithIdentifier:URL:options:error:。在使用时,必须指定使用文件附件的内容,而且文件格式必须是支持的类型之一。建立附件后,将其分配给内容对象的附件属性。 (对于远程通知,您必须从您的服务扩展作到这一点。)
附件通知支持的类型以下图:


QQ20160918-3.png-45.5kB

下面是建立UNNotificationAttachment的方法:

+ (nullable instancetype)attachmentWithIdentifier:(NSString *)identifier URL:(NSURL *)URL options:(nullable NSDictionary *)options error:(NSError *__nullable *__nullable)error;

注意:URL必须是一个有效的文件路径,否则会报错

这里我再在说下options的属性,一共有4种选项(这几个属性可研究死我了)

  • 1UNNotificationAttachmentOptionsTypeHintKey此键的值是一个包含描述文件的类型统一类型标识符(UTI)一个NSString。若是不提供该键,附件的文件扩展名来肯定其类型,经常使用的类型标识符有
    kUTTypeImage,kUTTypeJPEG2000,kUTTypeTIFF,kUTTypePICT,kUTTypeGIF ,kUTTypePNG,kUTTypeQuickTimeImage等。看到这里你必定有疑问,这些类型导入报错了啊!!我研究了苹果文档,发现你们须要添加如下框架才能够,具体你们能够经过如下类型来处理。

注意:
框架就是#import<MobileCoreServices/MobileCoreServices.h>

使用方法以下:

dict[UNNotificationAttachmentOptionsTypeHintKey] = (__bridge id _Nullable)(kUTTypeImage);
  • 2UNNotificationAttachmentOptionsThumbnailHiddenKey,是一个BOOL值,为YES时候,缩略图将隐藏,默认为YES。如图:


    IMG_0124.PNG-67.2kB


    你们能够对照上面的图来看,就明白是哪里的图消失了。

    使用方法以下:

    dict[UNNotificationAttachmentOptionsThumbnailHiddenKey] = @YES;
  • 3UNNotificationAttachmentOptionsThumbnailClippingRectKey剪贴矩形的缩略图。这个密钥的值是包含一个归一化的CGRect - 也就是说,一个单元的矩形,其值是在以1.0〜 0.0 ,表示要显示的原始图像的所述部分的字典。例如,指定的(0.25 , 0.25)的原点和大小(0.5 ,0.5 )定义了剪辑矩形,只显示图像的中心部分。使用CGRectCreateDictionaryRepresentation函数来建立字典的矩形。

上面这句话是苹果的翻译,太绕口了。我简单说,就是我下面这幅图。


QQ20160919.png-728.4kB


整张图被分割了,总体比例为1,若是想获得图中阴影面积,就须要写的CGRect(0.5,0.5,0.25,0.25),意思是,从(0.5,0.5)为原点,面积为(0.25,0.25),你们能够理解成,即下面的方法。

使用方法以下:

dict[UNNotificationAttachmentOptionsThumbnailClippingRectKey] = (__bridge id _Nullable)((CGRectCreateDictionaryRepresentation(CGRectMake(0.5, 0.5, 0.25 ,0.25))));;

使用上面的方法,能够获得一张图的阴影部分的图像,这张图像会是通知的缩略图。好比我下面的这个图,缩略图你们应该能够发现变了吧。


QQ20160919-10.png-49.2kB

这里为了理解,在给你们说几个"坐标点":
(0,0,0.25,0.25)左上角的最小正方形
(0,0,0.5,0.5) 四分之一的正方形,左上角
(0.5,0.5,0.5,0.5)四分之一的正方形,右下角
(0.5,0,0.5,0.5)四分之一的正方形,左下角
(0.25,0.25,0.5,0.5)最中心的正方形

特别注意:

调试到这里的时候,我感受苹果应该是有个bug,就是我在来回变化这个显示缩略图的frame的时候,来回改,永远显示为第一次写的frame。我在修改UNNotificationRequest的requestIdentifer属性后,能够变换属性。因此我猜想可能相同requestIdentifer的通知,算一个通知,因此只能调用更新的方法,来变化缩略图的吃不腻吧,或许也不是bug。

  • 4UNNotificationAttachmentOptionsThumbnailTimeKey,通常影片附件会用到,指的是用影片中的某一秒来作这个缩略图;

使用方法以下:

dict[UNNotificationAttachmentOptionsThumbnailTimeKey] =@10;

这里咱们能够直接传递一个NSNumber的数值,好比使用影片第10s的画面来作缩略图就按照上面的来写。此外,要注意的是,这个秒数必须是这个影片长度范围内的,否则报错。

3.UNTimeIntervalNotificationTrigger (通知触发模式)

这个我在iOS开发 iOS10推送必看(基础篇)这篇文章中已经初步介绍了,如今我在详细介绍下。

  • 1.UNPushNotificationTrigger (远程通知触发)通常咱们不会使用的

  • 2.UNTimeIntervalNotificationTrigger (本地通知) 必定时间以后,重复或者不重复推送通知。咱们能够设置timeInterval(时间间隔)和repeats(是否重复)。

    使用方法:

    UNTimeIntervalNotificationTrigger *triggerOne = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

解释:上面的方法是指5秒钟以后执行。repeats这个属性,若是须要为重复执行的,则TimeInterval必须大于60s,不然会报`* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating'`的错误!**

  • 3.UNCalendarNotificationTrigger(本地通知) 必定日期以后,重复或者不重复推送通知 例如,你天天8点推送一个通知,只须要dateComponents为8。若是你想天天8点都推送这个通知,只要repeats为YES就能够了。
// 周一早上 8:00 上班 NSDateComponents *components = [[NSDateComponents alloc] init]; // 注意,weekday是从周日开始的,若是想设置为从周一开始,你们能够本身想一想~ components.weekday = 2; components.hour = 8; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
  • 4.UNLocationNotificationTrigger (本地通知)地理位置的一种通知,使用这个通知,你须要导入
    #import<CoreLocation/CoreLocation.h>这个系统类库。示例代码以下:
//一、若是用户进入或者走出某个区域会调用下面两个方法 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region代理方法反馈相关信息 //二、一到某个经纬度就通知,判断包含某一点么 // 不建议使用!!!!!!CLRegion *region = [[CLRegion alloc] init];// 不建议使用!!!!!! CLCircularRegion *circlarRegin = [[CLCircularRegion alloc] init]; [circlarRegin containsCoordinate:(CLLocationCoordinate2D)]; UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:circlarRegin repeats:NO];

注意,这里建议使用CLCircularRegion这个继承自CLRegion的类,由于我看到苹果已经飞起了CLRegion里面是否包含这一点的方法,而且推荐咱们使用CLCircularRegion这个类型



 
文/徐不一样(简书做者) 原文连接:http://www.jianshu.com/p/f5337e8f336d 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。
相关文章
相关标签/搜索