ios10新特性-UserNotification

ios10新特性-UserNotification

引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是UILocationNotification类,远程通知有苹果服务器进行转发,本地通知和远程通知其回调的处理都是通过AppDelegate中的几个回调方法来完成。iOS10系统中,通知功能的增强是一大优化之处,iOS10中将通知功能整合成了一个框架UserNotification,其结构十分类似于iOS8中的UIWebView向WebKit框架整合的思路。并且UserNotification相比之前的通知功能更加强大,主要表现在如下几点:

1.通知处理代码可以从AppDelegate中剥离。

2.通知的注册,设置,处理更加结构化,更易于模块化开发。

3.UserNotification支持自定义通知音效和启动图。

4.UserNotification支持向通知内容中添加媒体附件,例如音频,视频。

5.UserNotification支持开发者定义多套通知模板。

6.UserNotification支持完全自定义的通知界面。

7.UserNotification支持自定义通知中的用户交互按钮。

8.通知的触发更加容易管理。

从上面列举的几点就可以看出,iOS10中的UsreNotification真的是一个大的改进,温故而知新,关于iOS之前版本本地通知和远程通知的相关内容请查看如下博客:

本地推送:http://my.oschina.net/u/2340880/blog/405491

远程推送:http://my.oschina.net/u/2340880/blog/413584

更多详细内容可以参考这篇博客:https://my.oschina.net/u/2340880/blog/747781

demo参考:http://www.open-open.com/lib/view/open1472632538972.html

下面就是我自己写的小程序,小试牛刀一下:

第一步必不可少的肯定是要导入我们的头文件:<UserNotifications/UserNotifications.h>

然后在AppDelegate.m中注册通知(第一次写的时候就是没有注册通知,直接就写了,所以导致通知总是不显示)

#import "AppDelegate.h"

#import "ViewController.h"


#import <UserNotifications/UserNotifications.h>


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];




 ViewController *vc = [[ViewController alloc] init];


 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

 self.window.rootViewController = nav;


[self.window makeKeyAndVisible];



 //注册本地推送

 // 使用 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) {


    }];

//

//    [self addLocationNotification:5];


 return YES;

}


#pragma mark - UNUserNotificationCenterDelegate 

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

 //1. 处理通知


 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式

completionHandler(UNNotificationPresentationOptionAlert);

}

2.然后在ViewController.m文件里发送通知

#import "ViewController.h"

#import <UserNotifications/UserNotifications.h>


@interface ViewController ()

@property (nonatomic, strong) NSString *notitle;//通知标题

@property (nonatomic, strong) NSString *content;//通知内容

@property (nonatomic, strong) NSURL *lineUrl;//跳转链接

@property (nonatomic, strong) NSURL *imageUrl;//附加的图片

@property (nonatomic, strong) NSData *soundData;//声音


@end


@implementation ViewController


- (void)viewDidLoad {

[super viewDidLoad];


 self.title = @"新通知测试";


 self.view.backgroundColor = [UIColor whiteColor];


[self setUpUI];

}


- (void)setUpNotification {

 //初始化通知

 UNMutableNotificationContent *noContent = [[UNMutableNotificationContent alloc] init];

noContent.title = _notitle;//标题

noContent.subtitle = @"副标题";//副标题

noContent.body = _content;//正文

noContent.badge = @1;//


 UNNotificationSound *sound = [UNNotificationSound defaultSound];

noContent.sound = sound;

noContent.categoryIdentifier = @"uid";


//5秒后推送通知

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


 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"push" content:noContent trigger:trigger1];


 //通知

 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

 NSLog(@"%@  error",error);

    }];

}



- (void)setUpUI {

 _notitle = @"通知标题:iOS10测试";

 _content = @"这是一条紧急通知";

 _lineUrl = [NSURL URLWithString:@"http://www.cnblogs.com/zrr-notes/"];


 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

[btn setTitle:@"发送通知" forState:UIControlStateNormal];

[btn setBackgroundColor:[UIColor redColor]];

btn.frame = CGRectMake(100, 100, 100, 50);

[self.view addSubview:btn];

[btn addTarget:self action:@selector(setUpNotification) forControlEvents:UIControlEventTouchUpInside];


}

猜你喜欢

转载自blog.csdn.net/weixin_33725515/article/details/90802215