iOS 本地通知实现

作者: 风儿吹啊吹 | 来源:发表于2019-11-25 23:12 被阅读0次

1、导入所需的 库

#import <UserNotifications/UserNotifications.h>

2、注册通知,代理实现

// 代理
@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end

// 注册通知

- (void)registerNotification {
    UNAuthorizationOptions options = UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge;
    UNUserNotificationCenter.currentNotificationCenter.delegate = self;
    [UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            // 允许授权
        } else {
            // 不允许授权
        }
    }];
    
    
    // 获取用户对通知的设置
    // 通过settings.authorizationStatus 来处理用户没有打开通知授权的情况
    [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@",settings);
    }];
}

// 在前台时 收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    
    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
}


// 点击通知,从后台进入前台
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    NSString *identifier =  response.actionIdentifier;
    
    if ([identifier isEqualToString:@"open"]) {
        NSLog(@"打开操作");
    } else if ([identifier isEqualToString:@"close"]) {
        NSLog(@"关闭操作");
    }
    completionHandler();
}

3、添加通知、包含一些操作


- (void)addNotification {
    // 创建一个通知内容
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.badge = @1;
    content.title = @"title";
    content.subtitle = @"subtitle";
    content.body = @"body";
    content.sound = [UNNotificationSound defaultSound];
    content.categoryIdentifier = @"category";
    
    
    // 通知触发器
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:false];
    // 通知请求
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"noti" content:content trigger:trigger];
    //添加通知
    [UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        
        NSLog(@"error:%@",error);
    }];
    
    
    // 添加通知的一些操作
    UNNotificationAction *openAction = [UNNotificationAction actionWithIdentifier:@"open" title:@"打开" options:UNNotificationActionOptionForeground];
    UNNotificationAction *closeAction = [UNNotificationAction actionWithIdentifier:@"close" title:@"关闭" options:UNNotificationActionOptionDestructive];
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[openAction, closeAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
        
    NSSet *sets = [NSSet setWithObject:category];
    [UNUserNotificationCenter.currentNotificationCenter setNotificationCategories:sets];
}

效果图.png

4、移除通知

- (void)removeNotifiation {
    // 移除 待处理的通知
    [UNUserNotificationCenter.currentNotificationCenter removePendingNotificationRequestsWithIdentifiers:@[@"noti"]];
    // 移除 已经处理的通知
    [UNUserNotificationCenter.currentNotificationCenter removeDeliveredNotificationsWithIdentifiers:@[@"noti"]];
    
    // 移除所有的通知
    [UNUserNotificationCenter.currentNotificationCenter removeAllDeliveredNotifications];
    [UNUserNotificationCenter.currentNotificationCenter removeAllPendingNotificationRequests];
}

相关文章

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

  • iOS 本地通知实现

    1、导入所需的 库 2、注册通知,代理实现 3、添加通知、包含一些操作 4、移除通知

  • iOS本地通知的实现

    通知的原理,在这里就不做过多的解释了,不懂的朋友可以自行Google 这里我们主要说下本地推送。目前来说本地通知主...

  • iOS本地通知实现思路

    最近忙毕业的事情,到了6月底才忙完,刚毕业,然后又是公司推新版本,很累很骚,所以到现在才有时间更新博客,其实也是因...

  • iOS14开发- 通知

    iOS 中的通知主要分为 2 种,本地通知和远程通知。 本地通知 使用步骤 导入UserNotifications...

  • ios10新特性-UserNotification

    ios10新特性-UserNotification 引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是...

  • iOS UserNotifications通知管理---本地通知

    iOS UserNotifications通知管理---本地通知篇 iOS 10对以前混乱的和通知相关的API进行...

  • IOS的通知

    通知详解 简书-iOS10 推送通知 UserNotifications iOS10本地通知UserNotifi...

  • iOS推送通知概览

    iOS推送通知概览 一、响应推送(本地通知 和 远程通知 都合适) iOS 10 以前 1. UIUserNoti...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

网友评论

    本文标题:iOS 本地通知实现

    本文链接:https://www.haomeiwen.com/subject/iveswctx.html