iOS 本地推送闹钟功能实现

在AppDelegate中导入

#import 

//注册本地通知
-(void)registerLocalNotification:(NSDictionary *)launchOptions application:(UIApplication *)application {
    //注册通知
    if (@available(iOS 8, *)) {
        if (UIDevice.currentDevice.systemVersion.floatValue >=10.0) {
              if (@available(iOS 10, *)) {
                      UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
                       //请求获取通知权限(角标,声音,弹框)
                       [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |
                                                                UNAuthorizationOptionSound |
                                                                UNAuthorizationOptionAlert)
                                             completionHandler:^(BOOL granted, NSError * _Nullable error) {
                           if (granted) {
                               //获取用户是否同意开启通知
                               DDLog(@"开启通知成功!");
                           }
                       }];
              }
        }else{
              // *注册本地通知, ios8之后必须要注册*
              if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
                  [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
              }
        }
    }
}

//进入前台移除通知角标
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0]; //清除角标
    [[UIApplication sharedApplication] cancelAllLocalNotifications];//清除APP所有通知消息
}

//添加通知移除通知参考demo
demo地址:https://github.com/mrZombie2016/ZBLocalNotification

创建时间为date的每天重复提醒例子:
导入

#import "ZBLocalNotification.h"

#pragma mark  - 添加删除通知
-(void)addLocalNotification {
         // 设置一个每天都提醒的推送,传入的时间设置为:
           NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
           [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
           NSDate * date = [formatter dateFromString:@"2019-12-02 08:00:00"];
            [ZBLocalNotification createLocalNotificationWithAttribute:
                                            @{ZBNotificationUserInfoName:@"notificationNameMorning",
                                              ZBNotificationSoundName:ZBNotificationSoundAlarm,
                                              ZBNotificationAlertBody:@"妈妈再也不用担心扔垃圾啦!",
                                              ZBNotificationAlertTitle:@"扔垃圾啦!扔垃圾啦!扔垃圾啦!重要的事情说三遍!",
                                              ZBNotificationFireDate:date,
                                              ZBNotificationPriority:@(0),
                                              ZBNotificationRepeat:@(ZBLocalNotificationRepeatEveryDay)}];
       date = [formatter dateFromString:@"2019-12-02 18:00:00"];
        [ZBLocalNotification createLocalNotificationWithAttribute:
                                            @{ZBNotificationUserInfoName:@"notificationNameNight",
                                              ZBNotificationSoundName:ZBNotificationSoundAlarm,
                                              ZBNotificationAlertBody:@"妈妈再也不用担心扔垃圾啦!",
                                              ZBNotificationAlertTitle:@"扔垃圾啦!扔垃圾啦!扔垃圾啦!重要的事情说三遍!",
                                              ZBNotificationFireDate:date,
                                              ZBNotificationPriority:@(0),
                                              ZBNotificationRepeat:@(ZBLocalNotificationRepeatEveryDay)}];
}

   //设置取消本地推送
-(void)cancelLocalNotification {
    [ZBLocalNotification cancelLocalNotificationWithName:@"notificationNameMorning"];
    [ZBLocalNotification cancelLocalNotificationWithName:@"notificationNameNight"];
}

参考链接:https://blog.csdn.net/weixin_42857581/article/details/85399721

你可能感兴趣的:(iOS 本地推送闹钟功能实现)