1、简介

  本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时、待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知。

2、创建UILocalNotification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. UILocalNotification *localNotifi = [UILocalNotification new];
localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:];//发送时间
localNotifi.alertBody = @"这是一条本地推送";//设置提醒内容
localNotifi.soundName = UILocalNotificationDefaultSoundName;//设置推送声音
localNotifi.applicationIconBadgeNumber = ;//设置app右上角图标标记
// localNotifi.hasAction = YES;//锁屏时是否显示内容,默认yes 设置提醒按钮文字
// localNotifi.alertAction = @"好的";//按钮文字
localNotifi.timeZone = [NSTimeZone defaultTimeZone];//设置时区 [NSTimeZone defaultTimeZone],跟随手机的时区
localNotifi.repeatInterval = NSCalendarUnitMinute;//设置重复每隔多久发送一次 最小单位分钟 。0代表不重复,此属性设置了, 那么调度池不会用完释放!需要手动删除通知对象
localNotifi.repeatCalendar = [NSCalendar calendarWithIdentifier:@"NSCalendarIdentifierChinese"];//设置依赖的日历历法,默认就是跟随系统走,历法不一样每月重复间隔时间也不一样(如农历是30天)
if (@available(iOS 8.2, *)) {
localNotifi.alertTitle = @"本地推送呵呵";
} else {
// Fallback on earlier versions
}//设置弹出框标题
UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];//使用可变子类
category.identifier = @"分类";//设置标识符,注意与发送通知设置的category标识符一致
// 设置前台按钮,点击后能使程序回到前台的叫做前台按钮
UIMutableUserNotificationAction *actionLeft = [UIMutableUserNotificationAction new];
actionLeft.identifier = @"left";
actionLeft.activationMode = UIUserNotificationActivationModeForeground;
actionLeft.title = @"前台按钮";
// 设置后台按钮,点击后程序还在后台执行,如QQ的消息
UIMutableUserNotificationAction *actionRight = [UIMutableUserNotificationAction new];
actionRight.identifier = @"right";
actionRight.activationMode = UIUserNotificationActivationModeBackground;
actionRight.title = @"后台按钮";
[category setActions:@[actionLeft,actionRight] forContext:UIUserNotificationActionContextDefault];
NSSet *catogorySet = [NSSet setWithObject:category];
localNotifi.category = @"分类";
// 在哪个区域发送通知, 进入这个区域就发送这个通知,可以进来调一次,出去调一次
// @property(nullable, nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
// @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);区域是否只检测一次
// @property(nullable, nonatomic,copy) NSString *alertLaunchImage;设置启动图片
// @property(nullable, nonatomic,copy) NSDictionary *userInfo;推送携带参数
// @property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);添加下拉快速回复功能 if (iOS8_OR_LATER) {
// UIUserNotificationType 枚举:
// UIUserNotificationTypeNone = 0,
// UIUserNotificationTypeBadge = 1 << 0, //图标标记
// UIUserNotificationTypeSound = 1 << 1, //声音
// UIUserNotificationTypeAlert = 1 << 2, //提醒
//
// categories:用于添加下拉快速回复功能
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:catogorySet];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} // 调度本地推送通知(调度后在特定时间fireData发出)
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotifi]; // 立即发送本地通知
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotifi]; // 处理退出后通知的点击,程序启动后获取通知对象,如果是首次启动还没有发送通知,那第一次通知对象为空,没必要去处理通知
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotifi = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
[self changeLocalNotifi:localNotifi];
} return YES;
}

3、移除推送通知

- (void)deleteLocalNotifi{
// 删除所有通知
[[UIApplication sharedApplication] cancelAllLocalNotifications]; // 删除指定通知(发出过期的推送不在此数组)
NSArray *notifiArr = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *localNoti in notifiArr) {
//根据UserInfo的值,来查看这个是否是你想要删除的通知
if (localNoti.userInfo) {
[[UIApplication sharedApplication] cancelLocalNotification:localNoti];
}
}
}

4、就收到推送处理方法

//处理通知
- (void)changeLocalNotifi:(UILocalNotification *)localNotifi{
// 如果在前台直接返回
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
return;
}
// 获取通知信息
// localNotifi.userInfo
}
//接收到本地通知后调用 程序前台或后台的时候才有用,退出无法接收到消息
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
[self changeLocalNotifi:notification];
}

5、前台和后台按钮处理方法

//前台和后台按钮处理方法
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)(void))completionHandler{
// 我们可以在这里获取标识符,根据标识符进行判断是前台按钮还是后台按钮还是神马按钮,进行相关逻辑处理(如回复消息)
NSLog(@"identifier : %@",identifier);
// 一旦接受必须调用的方法(告诉系统什么时候结束,系统自己对内部进行资源调配)
completionHandler();
}

6、远程推送

  https://www.jianshu.com/p/25e267037f6e

最新文章

  1. mysql join 和left join 对于索引的问题
  2. jQuery.first() 函数
  3. BendFord&#39;s law&#39;s Chi square test
  4. iOS单例模式(Singleton)写法简析
  5. laravel transaction : laravel 的事务是不支持eloquent的, 要用DB::的方式
  6. Android 调用webService(.net平台)
  7. 自动化测试——case编写
  8. HOJ 2245 浮游三角胞(数学啊 )
  9. Nikto and whatweb
  10. 编译 pcre - 开源的正则表达式(库)
  11. Jsの练习-数组常用方法
  12. k8s常用命令
  13. js对象深拷贝
  14. DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
  15. Python 百分比计算
  16. git 标签管理
  17. 07_Java基础语法_第7天(练习)_讲义
  18. select case when if 的一些用法
  19. SDWebImage从缓存中获取图片
  20. python爬虫:爬取慕课网视频

热门文章

  1. css布局-瀑布流的实现
  2. iOS开发系列-weak与unsafe_unretained修饰符
  3. vc枚举本机端口信息
  4. Estimation
  5. Windows Server 2003服务器清理C盘空间的资料多个解决方法
  6. Leetcode - K Sum
  7. teb_local_planner安装及使用
  8. 手工编写JavaWeb项目
  9. PAT甲级——A1114 Family Property【25】
  10. java模拟页面表单登录