iOS本地通知的实现
来源:互联网 发布:linux arp -n 编辑:程序博客网 时间:2024/06/02 01:49
一、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(@"设置成功"); }];
阅读全文
0 0
- iOS本地通知的实现
- iOS 本地通知的实现
- iOS本地通知的实现
- iOS 本地通知的实现
- iOS实现本地通知
- IOS实现本地通知
- iOS实现本地通知
- iOS实现本地通知
- IOS实现本地通知
- IOS本地通知实现
- ios-实现本地通知(UILocalNotification)
- ios 本地通知(UILocalNotification)的学习和使用(通过本地通知实现提醒功能)
- iOS实现本地通知(local notification)的方法
- 【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛
- (七十三)iOS本地推送通知的实现
- iOS的本地通知tishikuang
- iOS中 本地通知/本地通知详解 韩俊强的博客
- iOSAPP本地通知的实现
- JavaSE 回顾与总结
- 8月7日小结
- 超大型LED显示屏(模拟)
- Spring实战笔记——运行时值注入
- SDR学习笔记(一)
- iOS本地通知的实现
- 最大公约数
- 大数据基础知识
- JS_Say程序仿QQ侧滑部分
- Piggy-Bank HDU
- 剑客决斗(flag)
- shiro学习(一)
- nopCommerce 3.9 大波浪系列 之 路由扩展 [多语言Seo的实现]
- 学习Cocos的一些链接