iOS开发本地推送(iOS10)UNUserNotificationCenter
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的更多相关文章
- iOS开发本地推送
1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...
- IOS 本地推送 IOS10.0以上 static的作用 const的作用
//需要在AppDelegate里面启动APP的函数 加上 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNot ...
- iOS的本地推送删除不了解决方法
最近在研究苹果推送,当测试本地推送的时候,发现一个问题,就是一旦你添加了一个本地推动的通知,当你修改代码,删除应用,当你再次运行app,它还是会在横幅上面弹出推送,尼玛怎么搞都删除不了,近乎崩溃了,开 ...
- iOS关于本地推送
不多说 直接上代码 NSDate *now = [NSDate date]; UILocalNotification *reminderNotification = [[UILocalNoti ...
- iOS开发,推送消息 steps
概述:推送过程简介 一.App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远程推送的申请. ...
- iOS开发——消息推送跳转
项目开发用集成是极光推送JPush 这里主要是消息推送过来处理对应界面跳转 同时看到两篇写的不错的相关博客分享一下: http://www.jianshu.com/ ...
- iOS开发——极光推送
1.到极光官网 https://www.jpush.cn/ 下载极光推送SDK. 具体如何集成最好参考官网的文档,以及一些失败的原因.文档非常详细,我也是参考集成的. 2.到极光推送官网注册自己的应用 ...
- iOS开发消息推送原理
转载自:http://www.cnblogs.com/cdts_change/p/3240893.html 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Prov ...
- iOS之本地推送(前台模式与后台模式)
#import "AppDelegate.h" #import "GlobalDefine.h" @interface AppDelegate () @end ...
随机推荐
- 【校OJ】选网线
暑假学校OJ上的题目. 一道很有意思的二分. 题意:三个数组,每个数组各选一个数出来看是否能组成目标数. 题解:前两个数组两两的和组合一下,二分第三个数组,找是否能组成目标数. 代码: #includ ...
- ARM GNU 专有符号
1. @ 表示注释从当前位置到行尾的字符. 2. # 注释掉一整行. 3. ; 新行分隔符.
- jupyter中使用graphviz
参考: https://www.cnblogs.com/zhanjiahui/p/11335038.html https://blog.csdn.net/linxid/article/details/ ...
- MySQL入门基础知识
1.MySQL环境变量的配置 操作数据库时,要进入bin目录,如下: 但是如果进行配置环境变量,就不必切换路径,如下图所示,即使没有在G:\mysql-8.0.16-winx64\bin下,数据库依然 ...
- Python学习笔记(一)——输入与输出
输出:——print() Python中的输出使用print()完成 >>> 在屏幕中输出Hello World >>> print('Hello World') ...
- BCZM : 1.8
问题: 所有的员工均在1楼进电梯的时候,选择所要到达的楼层.然后计算出停靠的楼层i,当到达楼层i的时候,电梯停止.所有人走出电梯,步行到所在的楼层中.求所有人爬的楼层数目和的最小值. 解法一 ...
- soapui打开即报错------连接不上Internet
1.遇到的问题: 打开soapui即报错,如下: You're getting this message since your computer is offline and SoapUI can't ...
- leetcode-11-盛水最多的容器
题目描述: 方法一:双指针 class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(h ...
- [JZOJ6271] 2019.8.4【NOIP提高组A】锻造
题目 题目大意 武器的每个级别有固定的两种属性\(b_i\)和\(c_i\) 可以用\(a\)的代价得到一把\(0\)级的武器. 可以将\(x\)级武器和\(y=\max(x-1,0)\)级武器融合锻 ...
- socket 上传文件
""" "" server.py """服务端 """import socketimpor ...