博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发本地推送(iOS10)UNUserNotificationCenter
阅读量:6456 次
发布时间:2019-06-23

本文共 9060 字,大约阅读时间需要 30 分钟。

1、简介

  iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了。简单介绍本地推送的使用!

 

2、简单使用UNUserNotificationCenter

  一、创建UNUserNotificationCenter,设置推送模式和代理!

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)                              completionHandler:^(BOOL granted, NSError * _Nullable error) {                                  if (!error) {                                      NSLog(@"succeeded!");                                  }                              }];        center.delegate = self;

  二、设置推送内容

UNMutableNotificationContent *content = [UNMutableNotificationContent new];        content.title = @"推送中心标题";        content.subtitle = @"副标题";        content.body  = @"这是UNUserNotificationCenter信息中心";        content.badge = @20;        content.categoryIdentifier = @"categoryIdentifier";        //        需要解锁显示,红色文字。点击不会进app。//        UNNotificationActionOptionAuthenticationRequired = (1 << 0),////        黑色文字。点击不会进app。//        UNNotificationActionOptionDestructive = (1 << 1),////        黑色文字。点击会进app。//        UNNotificationActionOptionForeground = (1 << 2),                UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"enterApp"                                                                            title:@"进入应用"                                                                          options:UNNotificationActionOptionForeground];        UNNotificationAction *clearAction = [UNNotificationAction actionWithIdentifier:@"destructive"                                                                                 title:@"忽略2"                                                                               options:UNNotificationActionOptionDestructive];        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier"                                                                                  actions:@[action,clearAction]                                                                        intentIdentifiers:@[requestID]                                                                                  options:UNNotificationCategoryOptionNone];        [center setNotificationCategories:[NSSet setWithObject:category]];

  三、设置推送方式

UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestID content:content trigger:timeTrigger];

  trigger的其它用法:

//1分钟后提醒        UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];                //每小时重复 1 次        UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 repeats:YES];                //周日早8点        NSDateComponents *components = [[NSDateComponents alloc] init];        components.weekday = 1;        components.hour = 8;        UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];                //#import 
CLRegion *region = [[CLRegion alloc] init]; UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];

 

  四、添加推送request

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

 

3、UNUserNotificationCenter的Delegate

//将要推送- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{    NSLog(@"----------willPresentNotification");}//已经完成推送- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{    NSLog(@"============didReceiveNotificationResponse");    NSString *categoryID = response.notification.request.content.categoryIdentifier;    if ([categoryID isEqualToString:@"categoryIdentifier"]) {        if ([response.actionIdentifier isEqualToString:@"enterApp"]) {            if (@available(iOS 10.0, *)) {                            } else {                // Fallback on earlier versions            }        }else{            NSLog(@"No======");        }    }    completionHandler();}

 

4、移除推送

[center removePendingNotificationRequestsWithIdentifiers:@[requestID]];        [center removeAllDeliveredNotifications];

附录:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    if (@available(iOS 10.0, *)) {        //第一步:获取推送通知中心        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)                              completionHandler:^(BOOL granted, NSError * _Nullable error) {                                  if (!error) {                                      NSLog(@"succeeded!");                                  }                              }];        center.delegate = self;                //第二步:设置推送内容        UNMutableNotificationContent *content = [UNMutableNotificationContent new];        content.title = @"推送中心标题";        content.subtitle = @"副标题";        content.body  = @"这是UNUserNotificationCenter信息中心";        content.badge = @20;        content.categoryIdentifier = @"categoryIdentifier";                //        需要解锁显示,红色文字。点击不会进app。//        UNNotificationActionOptionAuthenticationRequired = (1 << 0),////        黑色文字。点击不会进app。//        UNNotificationActionOptionDestructive = (1 << 1),////        黑色文字。点击会进app。//        UNNotificationActionOptionForeground = (1 << 2),                UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"enterApp"                                                                            title:@"进入应用"                                                                          options:UNNotificationActionOptionForeground];        UNNotificationAction *clearAction = [UNNotificationAction actionWithIdentifier:@"destructive"                                                                                 title:@"忽略2"                                                                               options:UNNotificationActionOptionDestructive];        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier"                                                                                  actions:@[action,clearAction]                                                                        intentIdentifiers:@[requestID]                                                                                  options:UNNotificationCategoryOptionNone];        [center setNotificationCategories:[NSSet setWithObject:category]];        //第三步:设置推送方式        UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestID content:content trigger:timeTrigger];                //第四步:添加推送request        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {                    }];                        [center removePendingNotificationRequestsWithIdentifiers:@[requestID]];        [center removeAllDeliveredNotifications];//        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {//            NSLog(@"settings===%@",settings);//        }];    } else {    }    return YES;}#pragma mark - UNUserNotificationCenterDelegate//将要推送- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{    NSLog(@"----------willPresentNotification");}//已经完成推送- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{    NSLog(@"============didReceiveNotificationResponse");    NSString *categoryID = response.notification.request.content.categoryIdentifier;    if ([categoryID isEqualToString:@"categoryIdentifier"]) {        if ([response.actionIdentifier isEqualToString:@"enterApp"]) {            if (@available(iOS 10.0, *)) {                            } else {                // Fallback on earlier versions            }        }else{            NSLog(@"No======");        }    }    completionHandler();}

 

转载于:https://www.cnblogs.com/xianfeng-zhang/p/8310394.html

你可能感兴趣的文章
已经上架的app在AppStore上搜不到的解决办法
查看>>
Hadoop日志以及日志的格式和命名组成
查看>>
Bootstrap3基础 栅格系统 col-lg/md/sm/xs-* 简单示例
查看>>
jsp+servlet+javaBean实现用户留言
查看>>
CSS盒模型
查看>>
解决网站使用sqlite时并发问题的一个经验
查看>>
operamasks—omBorderLayout布局
查看>>
代理模式
查看>>
彻底搞定C指针---指向指针的指针(转)
查看>>
Django多进程日志文件问题
查看>>
easyUI combobox combotree 模糊查询,带上下键选择功能,待完善。。。。
查看>>
RHEL6解决无法使用YUM源问题
查看>>
百度地址解析和逆地址解析
查看>>
【Dig工具】
查看>>
Jobs
查看>>
八皇后
查看>>
与图论的邂逅07:K短路
查看>>
Unix学习笔记(一) 用户安全与权限
查看>>
性能测试基础
查看>>
Eclipse rap 富客户端开发总结(1) :rap简单介绍和开发环境搭建
查看>>