UNUserNotificationCenter的基本使用

  1. 引入头文件
#import <UserNotifications/UserNotifications.h>
  1. 注册通知
/*
     注册通知(推送)
     申请App需要接受来自服务商提供推送消息
     */
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!granted) {
                [self showNoticeAlert];
            }
        }];
        
        // 注册远程通知
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
  1. 检查通知权限
- (void)checkNotificationAuth:(void(^)(BOOL granted))block
{
    if (@available(iOS 10.0, *)) {
        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
                block(YES);
            }else {
                block(NO);
            }
        }];
    }else {
        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone) {
            block(NO);
        }else {
            block(YES);
        }
    }
}
  1. 发送本地通知
if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = @"title";
        content.body = @"body";
        content.badge = 0;
        content.userInfo = @"userInfo";
        NSTimeInterval timeInterval = [[NSDate dateWithTimeIntervalSinceNow:1] timeIntervalSinceNow];
        // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
        UNNotificationRequest *reqeust = [UNNotificationRequest requestWithIdentifier:@"noticeIdentifier" content:content trigger:trigger];
        [center addNotificationRequest:reqeust withCompletionHandler:^(NSError * _Nullable error) {
            if (error) {
                DLog(@"error");
            }
        }];
    }

5.发送本地通知一定要实现以下代理方法

//App在前台时候回调:用户正在使用状态
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}
  1. 通知代理方法
// 推送消息回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
       // 远程推送消息处理
    } else {
       // 本地消息处理
    }
    
    completionHandler();
}

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 3,057评论 3 6
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 3,569评论 0 12
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 1,402评论 0 6
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 5,419评论 0 11
  • 这个秋天,你来看我带来一朵花,似落未落的样子思念的季节,你来看我带来一颗相思豆,似熟未熟的样子 我的目光,流连那个...
    碧漪阅读 622评论 0 59