UNUserNotificationCenter的基本使用

2019-08-28  本文已影响0人  Minoz_min
  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 = @{};
        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();
}
上一篇 下一篇

猜你喜欢

热点阅读