//1.appdelegate注册通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//注册通知
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{
//ios8 系统一下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
//注册获得device Token
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
//ios10 收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))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 前台收到远程通知:%@",userInfo);
}else{
//判断为本地通知
NSLog(@"ios10 前台手袋本地通知:%@",userInfo);
}
//执行这个方法,选择是否提醒用户有,badge、sound、alert、三种类型可以选择
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
//通知点击事件
- (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.title;//推送消息的标题
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]){
NSLog(@"ios 收到远程通知:%@",userinfo);
}else{
//判断为本地通知
NSLog(@"iOS10 收到本地通知:%@",userinfo);
}
}
//2.创建通知
- (void) createUserNotification{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = @"推送测试通知";
content.subtitle = @"通知测试";
content.body = @"来时推送测试的通知消息";
content.badge = @1;
NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:@"test.png" ofType:nil];
//设置通知附件的内容
UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if(error){
NSLog(@"attachment error %@",error);
}
content.attachments = @[att];
content.launchImageName = @"test";
//设置声音
UNNotificationSound *sound = [UNNotificationSound defaultSound];
content.sound = sound;
//触发模式
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
//设置UNNotificationRequest
NSString *requestIdentifer = @"TestRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger];
//把通知驾到UNUserNotificationCenter 到指定触发点会被触发
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"add notification error:%@",error);
}];
}
//参照链接