iOS10本地推送实践记录(Objective-C)

首先感谢喵神活久见的重构 - iOS 10 UserNotifications 框架解析霜神WWDC2016 Session笔记 - iOS 10 推送Notification新特性,文章对推送剖析的很彻底,本文仅仅是学习实践过程中的笔记(还用的Objective-C)。

发送通知

在一切开始之前,一定要#import <UserNotifications/UserNotifications.h>。因为iOS10中,推送通知的相关功能提取到了这个新加入的框架。

然后,就可以上代码了~

    // 申请通知权限
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        
        // A Boolean value indicating whether authorization was granted. The value of this parameter is YES when authorization for the requested options was granted. The value is NO when authorization for one or more of the options is denied.
        if (granted) {
            
            // 1、创建通知内容,注:这里得用可变类型的UNMutableNotificationContent,否则内容的属性是只读的
            UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
            // 标题
            content.title = @"柯梵通知";
            // 次标题
            content.subtitle = @"柯梵办公室通知";
            // 内容
            content.body = @"柯梵科技是一家以人为本,具有强烈社会责任感的公司。公司的最大愿景就是每个员工都能住上大房子,开上好车,实现逆袭高富帅、白富美的愿望。";
            
            self.badge++;
            // app显示通知数量的角标
            content.badge = @(self.badge);
            
            // 通知的提示声音,这里用的默认的声音
            content.sound = [UNNotificationSound defaultSound];
            
            NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"jianglai" withExtension:@"jpg"];
            UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageIndetifier" URL:imageUrl options:nil error:nil];
            
            // 附件 可以是音频、图片、视频 这里是一张图片
            content.attachments = @[attachment];
            
            // 标识符
            content.categoryIdentifier = @"categoryIndentifier";
            
            // 2、创建通知触发
            /* 触发器分三种:
             UNTimeIntervalNotificationTrigger : 在一定时间后触发,如果设置重复的话,timeInterval不能小于60
             UNCalendarNotificationTrigger : 在某天某时触发,可重复
             UNLocationNotificationTrigger : 进入或离开某个地理区域时触发
            */
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
            
            // 3、创建通知请求
            UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:@"KFGroupNotification" content:content trigger:trigger];
            
            // 4、将请求加入通知中心
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
                if (error == nil) {
                    NSLog(@"已成功加推送%@",notificationRequest.identifier);
                }
            }];
        }
        
    }];
复制代码

这样,发送通知的步骤就完成了,回到手机桌面(不特别设置的话,应用内是不展示通知的),5秒之后就可以收到一条通知了。如下图(解锁状态和锁屏状态):

要注意的是,一旦用户拒绝了这个请求,再次调用该方法也不会再进行弹窗,想要应用有机会接收到通知的话,用户必须自行前往系统的设置中为你的应用打开通知,而这往往是不可能的。因此,在合适的时候弹出请求窗,在请求权限前预先进行说明,而不是直接粗暴地在启动的时候就进行弹窗,会是更明智的选择。

这里我和瞄神的观点一致,所以在需要发送通知的地方才申请通知权限。

修改通知

修改内容,增加一个新的通知请求到消息中心,此请求和原来的标识符一样。

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
......

UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:@"KFGroupNotification" content:content trigger:trigger];
            
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
       if (error == nil) {
           NSLog(@"已成功更新推送%@",notificationRequest.identifier);
       }
}];
复制代码

这样收到的通知就变成修改之后的了。

取消通知

取消通知分两种,取消未发送的通知和移除已展示过的通知。

// 移除已展示过的通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
    
// 取消还未发送的通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
复制代码

添加Action

要发送一个带有action的通知,先注册category。

    // 输入动作
    UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"inputIndentifier" title:@"输入" options:UNNotificationActionOptionForeground textInputButtonTitle:@"回复" textInputPlaceholder:@"请回复"];
    
    // 接受动作
    UNNotificationAction *commitAction = [UNNotificationAction actionWithIdentifier:@"commitIndentifier" title:@"确定" options:UNNotificationActionOptionForeground];
    
    // 取消动作
    UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"cancelIndentifier" title:@"取消" options:UNNotificationActionOptionDestructive];
    
    // 自定义类型
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIndentifier" actions:@[inputAction,commitAction,cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    NSSet *set = [[NSSet alloc] initWithObjects:category, nil];
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:set];
复制代码

这里可以在程序启动的时候注册,后面发通知直接根据不同的标识符选择不同的category即可。

content.categoryIdentifier = @"categoryIndentifier";
复制代码

这时候发的通知如下图,多了三个按钮,点第一个输入按钮还能直接输入内容发送:

处理通知

UNUserNotificationCenterDelegate提供的两个代理方法,第一个方法主要用来决定是否在应用内展示通知。

// 如果在应用内展示通知 (如果不想在应用内展示,可以不实现这个方法)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    
    // 展示
    completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
    
//    // 不展示
//    completionHandler(UNNotificationPresentationOptionNone);
}
复制代码

第二个方法用来处理用户与推送的通知进行的交互。

// 对通知进行响应
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    
    // 根据类别标识符处理目标反应
    if ([response.notification.request.content.categoryIdentifier isEqualToString:@"categoryIndentifier"]) {
        [self handleResponse:response];
    }
    completionHandler();
}

- (void)handleResponse:(UNNotificationResponse *)response {
    
    NSString *actionIndentifier = response.actionIdentifier;
    
    // 处理留言
    if ([actionIndentifier isEqualToString:@"inputIndentifier"]) {
        
        UNTextInputNotificationResponse *input = (UNTextInputNotificationResponse *)response;
        NSLog(@"%@",input.userText);
        UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
        navVC.visibleViewController.view.backgroundColor = [UIColor redColor];
        
    }
    // 处理确定点击事件
    else if ([actionIndentifier isEqualToString:@"commitIndentifier"]) {
        
        NSLog(@"用户点确定了~~");
        UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
        navVC.visibleViewController.view.backgroundColor = [UIColor greenColor];
    }
    // 处理取消点击事件
    else {
        
        NSLog(@"用户点取消了~~");
        UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
        navVC.visibleViewController.view.backgroundColor = [UIColor yellowColor];
    }
}
复制代码

当然,要实现代理方法,前提得设置代理。这里我直接把KFADelegate设置为消息中心的代理。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self setNotificationCategorys];
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    
    return YES;
}
复制代码

因为涉及到打开应用的行为,所以实现了这个方法的 delegate 必须在 applicationDidFinishLaunching:  返回前就完成设置

自定义样式

新建一个Notification Content Extention,配置info.plist文件

然后在 - (void)didReceiveNotification:(UNNotification *)notification里取出通知的内容并赋给UI控件展示。

- (void)didReceiveNotification:(UNNotification *)notification {
    
    // 标题
    self.titleLbl.text = notification.request.content.title;
    // 次标题
    self.subLbl.text = notification.request.content.subtitle;
    // 内容
    self.label.text = notification.request.content.body;
    
    UNNotificationAttachment *attachment = notification.request.content.attachments.firstObject;
    if (attachment.URL.startAccessingSecurityScopedResource) {
        // 图片
        UIImage *temptImage = [[UIImage alloc] initWithContentsOfFile:attachment.URL.path];
        // 压缩避免图片过大展示不全
        NSData *imageData = UIImageJPEGRepresentation(temptImage, 1.0);
        UIImage *resultImage = [[UIImage alloc] initWithData:imageData];
        self.img.image = resultImage;
        [attachment.URL stopAccessingSecurityScopedResource];
    }
}
复制代码

效果:

待解决: 1、图片未加载全。(此问题经小岩同学的提醒,已解决,效果见上图) 2、布局不够漂亮。

TODO

到这里基于iOS10的本地推送基本上就实现了,还有些东西还没尝试,比如推送音频和视频······

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值