首页 技术 正文
技术 2022年11月6日
0 收藏 699 点赞 494 浏览 6419 个字

1、简介

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

iOS开发本地推送(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 = @;
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();
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906