iOS本地通知的实现

一、iOS8之前的本地通知实现

iOS8之前的版本无需系统注册本地通知(表现为用户手动允许接受通知),则本地通知实现较为简单。
1、设置一个通知并触发

localNotif.alertBody = @"test";
 //NSDictionary *userInfo = [NSDictionary dictionaryWithObject:EightPMNotification forKey:EightPMNotification];
localNotif.userInfo = userInfo;
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = 0;
localNotif.soundName = LocalNotificationSoundForSign;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

2、取消一个通知

// 获取所有本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
    NSDictionary *userInfo = notification.userInfo;
    if (userInfo) {
        // 根据设置通知参数时指定的key来获取通知参数
        NSString *info = userInfo[key];
        // 如果当天多个账号有效触发通知,index会累加,但是只能有一个有效,所以当取消的时候,要--
        if (info != nil) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            NSLog(@"YYDebug:取消%@",info);
            break;
        }
    }
}

3、程序还在后台,监听点击通知

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

4、程序终止,监听点击通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- //根据launchOption来判断是否是收到指定的通知
-     if (launchOptions) {
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    NSDictionary *localUserInfo = [notification userInfo];
    if ([[localUserInfo objectForKey:EightPMNotification] isEqualToString:EightPMNotification]||[[localUserInfo objectForKey:EveryDayNotification] isEqualToString:EveryDayNotification]) {
        self.launchOptions = launchOptions;
        NSLog(@"YYDebug:通过点击终止的App推送签到");
        [self setLocalNotificationsBySignTime];
    }
}

二、iOS10本地通知

首先需要注册,之前的通知都在UIKit框架中,现在在

#import <UserNotifications/UserNotifications.h>
还要遵循<UNUserNotificationCenterDelegate>

1、注册本地通知

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        NSLog(@"YY:允许通知触发");
    }else{
        NSLog(@"YY:不允许通知触发");
    }
}];

2、设置本地通知

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    //触发通知
    content.title = [NSString stringWithFormat:@"%@天气情况简报",self.cityName];
    content.body = [NSString stringWithFormat:@"今日天气%@,当前温度温度%@",dict2[@"type"],dict1[@"data"][@"wendu"]];
    content.sound = [UNNotificationSound defaultSound];
    content.userInfo = [NSDictionary dictionaryWithObject:key forKey:@"cityName"];
    NSInteger unitFlag = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    NSDateComponents *cmps = [[NSCalendar currentCalendar] components:unitFlag fromDate:remindDate];
    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:cmps repeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:key content:content trigger:trigger];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"设置成功");
    }];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值