iOS10对推送做了比较大的变更,主要有以下4点
- 1.推送的内容更加丰富,由之前的alert到现在的title subtitle body attachment
- 2.本地和远程推送全部是由trigger触发(更加面向对象)
- 3.可以为推送增加附件 如图片,音频,视频等
- 4.可以方便的更新推送的内容
下面来以本地推送为例,
1.在iOS10中首先要获取权限
#import <UserNotifications/UserNotifications.h>
遵守UNUserNotificationCenterDelegate
协议
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions: UNAuthorizationOptionBadge|UNAuthorizationOptionSound |UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"注册通知成功");
}else{
NSLog(@"注册通知失败");
}
}];
return YES;
}
此时运行的话会出现提示框选择allow
2.设置本地推送的内容
- (void)creatLocalUserNotification{
UNTimeIntervalNotificationTrigger *trigger =[ UNTimeIntervalNotificationTrigger triggerWithTimeInterval:8 repeats:NO];
//创建通知的内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = [NSString stringWithFormat:@"当前时间提醒 %@",[NSDate date]];
content.subtitle = [NSString stringWithFormat:@"超模VS网红模特--subtitle"];
content.body = @"我爱超模全国十强诞生夜--body";
content.badge = @1;
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = @"category";
NSString *path = [[NSBundle mainBundle]pathForResource:@"222" ofType:@"png"];
NSError *error = nil;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment " URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"error: %@",error);
}
content.attachments = @[attachment];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"request" content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"推送已经成功 ");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"推送成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[alert addAction:action2];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}
<<< 设置好了运行下,如下图所示
<<< 把通知往下拖拽下,如图所示可以看到attachment
3.额外补充
以上两点就可以设置一个简单的本地推送,当我们有其他额外的要求的时候,比如直接在推送的通知上面编辑以及程序运行在前台的时候要不要显示推送等,那么该如果做呢?
给UNNotificationCategory添加action 注意不同的options代表用户点击之后进行不同的操作
#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>
@interface NotificationAction : NSObject
+(void)addNotificationAction;
@end
#import "NotificationAction.h"
@implementation NotificationAction
+(void)addNotificationAction{
UNNotificationAction *look = [UNNotificationAction actionWithIdentifier:@"action.look" title:@"查看详情" options:UNNotificationActionOptionForeground];//点击action打开APP
UNNotificationAction *join = [UNNotificationAction actionWithIdentifier:@"action.join" title:@"我要参加" options:UNNotificationActionOptionAuthenticationRequired];//点击action需要解锁
UNNotificationAction *cancle = [UNNotificationAction actionWithIdentifier:@"action.cancle" title:@"我想静静" options:UNNotificationActionOptionDestructive];//显示为红色
UNTextInputNotificationAction *input = [UNTextInputNotificationAction actionWithIdentifier:@"action.input" title:@"输入" options:UNNotificationActionOptionForeground textInputButtonTitle:@"发送" textInputPlaceholder:@"Placeholder"];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[look,join,cancle,input] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObject:category]];
}
@end
然后在creatLocalUserNotification
或者AppDelegate
里面调用这个方法,此时的通知是下面这样的
上面的这些设置,当app在前台运行的时候当收到推送的时候就不会在屏幕上方显示了,那么是否可以让在前台的时候也展示推送的呢?
此时需要用到UNUserNotificationCenterDelegate
在下面的方法中设置一下就可以了
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound);
}