1、简介

  iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了。简单介绍本地推送的使用UserNotifications官方文档说明

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 = @;
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: repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestID content:content trigger:timeTrigger];

  trigger的其它用法:

        //1分钟后提醒
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: repeats:NO]; //每小时重复 1 次
UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: repeats:YES]; //周日早8点
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = ;
components.hour = ;
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; //#import <CoreLocation/CoreLocation.h>
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 = @;
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: 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();
}

iOS开发本地推送(iOS10)UNUserNotificationCenter的更多相关文章

  1. iOS开发本地推送

    1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...

  2. IOS 本地推送 IOS10.0以上 static的作用 const的作用

    //需要在AppDelegate里面启动APP的函数 加上 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNot ...

  3. iOS的本地推送删除不了解决方法

    最近在研究苹果推送,当测试本地推送的时候,发现一个问题,就是一旦你添加了一个本地推动的通知,当你修改代码,删除应用,当你再次运行app,它还是会在横幅上面弹出推送,尼玛怎么搞都删除不了,近乎崩溃了,开 ...

  4. iOS关于本地推送

      不多说 直接上代码 
 NSDate *now = [NSDate date]; UILocalNotification *reminderNotification = [[UILocalNoti ...

  5. iOS开发,推送消息 steps

    概述:推送过程简介 一.App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远程推送的申请. ...

  6. iOS开发——消息推送跳转

    项目开发用集成是极光推送JPush     这里主要是消息推送过来处理对应界面跳转          同时看到两篇写的不错的相关博客分享一下:      http://www.jianshu.com/ ...

  7. iOS开发——极光推送

    1.到极光官网 https://www.jpush.cn/ 下载极光推送SDK. 具体如何集成最好参考官网的文档,以及一些失败的原因.文档非常详细,我也是参考集成的. 2.到极光推送官网注册自己的应用 ...

  8. iOS开发消息推送原理

    转载自:http://www.cnblogs.com/cdts_change/p/3240893.html 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Prov ...

  9. iOS之本地推送(前台模式与后台模式)

    #import "AppDelegate.h" #import "GlobalDefine.h" @interface AppDelegate () @end ...

随机推荐

  1. 测试Tensorflow-GPU的例子

    import tensorflow as tf # import os # os.environ['TF_CPP_MIN_LOG_LEVEL']='2' a = tf.placeholder(tf.i ...

  2. Python从入门到精通视频(全60集)✍✍✍

    Python从入门到精通视频(全60集)  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看 ...

  3. Python匹马行天下之运算符

    什么事运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算 ...

  4. 树形结构_红黑树:平衡2X 哈夫曼树:最优2X

    红黑树:平衡2X 哈夫曼树:最优2X 红黑树 :TreeSet.TreeMap 哈夫曼树 1. 将w1.w2.…,wn看成是有n 棵树的森林(每棵树仅有一个结点): 2. 在森林中选出根结点的权值最小 ...

  5. mysql分区partition详解

    分区管理  论坛 1. RANGE和LIST分区的管理 针对非整形字段进行RANG\LIST分区建议使用COLUMNS分区.  RANGE COLUMNS是RANGE分区的一种特殊类型,它与RANGE ...

  6. 2018-9-29-Roslyn-通过-Nuget-引用源代码-在-VS-智能提示正常但是无法编译

    title author date CreateTime categories Roslyn 通过 Nuget 引用源代码 在 VS 智能提示正常但是无法编译 lindexi 2018-09-29 1 ...

  7. Java 基础 - Object.clone()深拷贝和浅拷贝

    作者:YSOcean 出处:http://www.cnblogs.com/ysocean/ 本文版权归作者所有,欢迎转载,但未经作者同意不能转载,否则保留追究法律责任的权利.   ---------- ...

  8. thinkphp switch标签

    用法: <switch name="变量" > <case value="值1" break="0或1">输出内容1 ...

  9. 多进程报错 EOFError: EOF when reading a line

    EOF的意思为:end of file 这个错误会在多进程中出现,是因为子进程中不能出现input,只能在父进程中使用. 结果:

  10. maven项目引入外部第三方jar包,引入、本地编译、第三方jar一起打到jar中、在linux机器中解决classnotfound(配置classpath),笔记整理。

    文章目录 引用的第三方jar的目录结构(示例) 引入第三方jar进行dependency使项目内能import 本地编译 第三方jar一起打到jar中 在linux机器中解决classnotfound ...