一、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(@"设置成功");
}];