博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发之本地推送、接收到推送的方法
阅读量:6906 次
发布时间:2019-06-27

本文共 8041 字,大约阅读时间需要 26 分钟。

我们有时候有需要本地通知的功能

本地推送通知也需要申请推送通知权限,具体步骤可看我的上一篇博客(关于推送权限申请)

1、添加本地推送的方法,需要判断iOS10.0和iOS8.0不同的方法

 

#import <UserNotifications/UserNotifications.h>

 
/** 添加本地推送通知*/+ (void)addLocalNotificationWithTitle:(NSString *)title subTitle:(NSString *)subTitle body:(NSString *)body timeInterval:(long)timeInterval identifier:(NSString *)identifier userInfo:(NSDictionary *)userInfo repeats:(int)repeats{    if (title.length == 0 || body.length == 0 || identifier.length == 0) {        return;    }    if (@available(iOS 10.0, *)) {        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];        // 标题        if (title.length) {            content.title = title;        }        if (subTitle.length) {            content.subtitle = subTitle;        }        // 内容        if (body.length) {            content.body = body;        }        if (userInfo != nil) {            content.userInfo = userInfo;        }        // 声音        // 默认声音        content.sound = [UNNotificationSound defaultSound];        // 添加自定义声音        //content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];        // 角标 (我这里测试的角标无效,暂时没找到原因)        content.badge = @1;        // 多少秒后发送,可以将固定的日期转化为时间        NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:timeInterval] timeIntervalSinceNow];        UNNotificationTrigger *trigger = nil;        // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错        if (repeats > 0 && repeats < 7) {            NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInterval];            // 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息            unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitWeekday | NSCalendarUnitMinute | NSCalendarUnitSecond;            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];            // 获取不同时间字段的信息            NSDateComponents* comp = [gregorian components:unitFlags fromDate:date];            NSDateComponents *components = [[NSDateComponents alloc] init];            components.second = comp.second;            if (repeats == 6) {                //每分钟循环            } else if (repeats == 5) {                //每小时循环                components.minute = comp.minute;            } else if (repeats == 4) {                //每天循环                components.minute = comp.minute;                components.hour = comp.hour;            } else if (repeats == 3) {                //每周循环                components.minute = comp.minute;                components.hour = comp.hour;                components.weekday = comp.weekday;            } else if (repeats == 2) {                //每月循环                components.minute = comp.minute;                components.hour = comp.hour;                components.day = comp.day;                components.month = comp.month;            } else if (repeats == 1) {                //每年循环                components.minute = comp.minute;                components.hour = comp.hour;                components.day = comp.day;                components.month = comp.month;                components.year = comp.year;            }            trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];        } else {            //不循环            trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];        }        // 添加通知的标识符,可以用于移除,更新等操作 identifier        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];        [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {            NSLog(@"ECKPushSDK log:添加本地推送成功");        }];    } else {        UILocalNotification *notif = [[UILocalNotification alloc] init];        // 发出推送的日期        notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];        if (title.length > 0) {            notif.alertTitle = title;        }        // 推送的内容        if (body.length > 0) {            notif.alertBody = body;        }        if (userInfo != nil) {            NSMutableDictionary *mdict = [NSMutableDictionary dictionaryWithDictionary:userInfo];            [mdict setObject:identifier forKey:@"identifier"];            notif.userInfo = mdict;        } else {            // 可以添加特定信息            notif.userInfo = @{
@"identifier":identifier}; } // 角标 notif.applicationIconBadgeNumber = 1; // 提示音 notif.soundName = UILocalNotificationDefaultSoundName; // 循环提醒 if (repeats == 6) { //每分钟循环 notif.repeatInterval = NSCalendarUnitMinute; } else if (repeats == 5) { //每小时循环 notif.repeatInterval = NSCalendarUnitHour; } else if (repeats == 4) { //每天循环 notif.repeatInterval = NSCalendarUnitDay; } else if (repeats == 3) { //每周循环 notif.repeatInterval = NSCalendarUnitWeekday; } else if (repeats == 2) { //每月循环 notif.repeatInterval = NSCalendarUnitMonth; } else if (repeats == 1) { //每年循环 notif.repeatInterval = NSCalendarUnitYear; } else { //不循环 } [[UIApplication sharedApplication] scheduleLocalNotification:notif]; }}

2、移除本地推送通知的方法

/** 移除某一个指定的通知*/+ (void)removeNotificationWithIdentifierID:(NSString *)noticeId{    if (@available(iOS 10.0, *)) {        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];        [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray
* _Nonnull requests) { for (UNNotificationRequest *req in requests){ NSLog(@"ECKPushSDK log: 当前存在的本地通知identifier: %@\n", req.identifier); } }]; [center removePendingNotificationRequestsWithIdentifiers:@[noticeId]]; } else { NSArray *array = [[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification *localNotification in array){ NSDictionary *userInfo = localNotification.userInfo; NSString *obj = [userInfo objectForKey:@"identifier"]; if ([obj isEqualToString:noticeId]) { [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; } } }}/** 移除所有通知*/+ (void)removeAllNotification{ if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllPendingNotificationRequests]; }else { [[UIApplication sharedApplication] cancelAllLocalNotifications]; }}

3、接收到通知处理

iOS10.0之前的接收方法,在Appdelegate中重写下面方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    //接收到本地通知方法}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    //接收到远程通知方法}

iOS10.0以后的接收推送方法

#import 
//首先需要设置代理UNUserNotificationCenterDelegateif ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center removeAllPendingNotificationRequests]; }//再实现UNUserNotificationCenterDelegate代理的方法- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ //应用在前台时候接收到本地推送通知、远程推送通知调用此方法}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{ //应用程序在后台,用户通过点击本地推送、远程推送进入app时调用此方法}

 

希望和大家多多交流哈,有不同意见和更好的方法欢迎大家留言相互学习、相互进步。

转载于:https://www.cnblogs.com/hecanlin/p/10898981.html

你可能感兴趣的文章
Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入
查看>>
Excel 统计IP
查看>>
Lucene全文检索之-Lucene基础
查看>>
Java基于Socket文件传输示例
查看>>
spring01
查看>>
三,移植uboot-支持NAND启动
查看>>
vs2017报错LNK2005和LNK1169: 符号已多次定义
查看>>
ssh文件传输命令:sz与rz命令
查看>>
代理与反向代理
查看>>
CentOS7 使用ifconfig命令 ENS33没有IP地址的解决办法
查看>>
PHP防止SQL注入的方法
查看>>
jmeter接口测试
查看>>
文件夹设置“以前的版本”功能(配置卷影副本)
查看>>
PHP:第二章——PHP中的while语句
查看>>
043——VUE中组件之使用.sync修饰符与computed计算属性实现购物车原理
查看>>
基于Lucene的查询索引
查看>>
华为的四幅广告
查看>>
swift 学习- 27 -- 访问控制
查看>>
python 异常
查看>>
微服务之容器化技术---kubernetes的安装
查看>>