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. Oracle Net Configuration Assistant failed异常的解决方案

    来自:http://blog.itpub.net/25851087/viewspace-1419440/ 分类: Oracle [环境参数]     Host OS::Win7 32bit     C ...

  2. 在linux 或docker中使用 system.drawing.common

    在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...

  3. 网页开发人员收藏的16款HTML5工具

    本文收集的20款优秀的 HTML5 Web 应用程序,值得添加到您的 HTML5 的工具箱中,他们能够帮助你开发前端项目更快.更容易. Initializr Initializr 是一个可以让你创建 ...

  4. linux段位进阶

    1.青铜: 1.Linux基础知识.基本命令(起源.组成.常用命令如cp.ls.file.mkdir等常见操作命令) 2.Linux用户及权限基础 3.Linux系统进程管理进阶 4.linux高效文 ...

  5. Leetcode976. Largest Perimeter Triangle三角形的最大周长

    给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. 如果不能形成任何面积不为零的三角形,返回 0. 示例 1: 输入:[2,1,2] 输出:5 示例 2 ...

  6. FCC——相关练习

    算法题目1:Seek and Destroy(摧毁数组) 金克斯的迫击炮! 实现一个摧毁(destroyer)函数,第一个参数是待摧毁的数组,其余的参数是待摧毁的值. 帮助资源: Arguments ...

  7. 【JZOJ6353】给(ca)

    description analysis 很妙的\(DP\) 设\(f[i][j]\)表示已经放了\(i\)个叶子节点.根到当前节点走了\(j\)步向左的方案数 考虑调整\(DP\)方式,钦定伸出左儿 ...

  8. JavaScript - 判断当前使用的浏览器类型

    <script> window.onload = function() { // 判断当前使用的浏览器类型 var browserType = navigator.userAgent.to ...

  9. 解析JQuery Ajax

    jQuery是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 先来看一些简单的方法,这些方法都是对jQuery.ajax()进行封 ...

  10. 牛客多校第六场 J Upgrading Technology dp

    题意: 有n个技能,一开始都是0级,第i个技能从j-1级升到j级,花费$c_{i,j}$,但是花费不一定是正的 所有的技能升到j级时,奖励$d_j$但是奖励也不一定是正的 题解: 用sum[i][j] ...