转发自:https://www.jianshu.com/p/e347f999ed95     //已经废除的

http://blog.csdn.net/three_zhang/article/details/70170215

    http://www.cocoachina.com/ios/20160926/17645.html   //详解篇

https://www.jianshu.com/p/c58f8322a278    //详解篇

在iOS10苹果废弃了之前的UILocalNotification,而采用了新的UserNotifications Framework来推送通知。现在先说一下iOS10之前的本地推送流程!

iOS 10之前

注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
} return YES;
}

发送通知

// 1.创建一个本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init]; // 1.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 1.2.设置通知内容
localNote.alertBody = @"这是一个本地推送"; // 1.3.设置锁屏时,字体下方显示的一个文字
localNote.alertAction = @"看我";
localNote.hasAction = YES; // 1.4.设置启动图片(通过通知打开的)
localNote.alertLaunchImage = @"../Documents/1.jpg"; // 1.5.设置通过到来的声音
localNote.soundName = UILocalNotificationDefaultSoundName; // 1.6.设置应用图标左上角显示的数字
localNote.applicationIconBadgeNumber = 1; // 1.7.设置一些额外的信息
localNote.userInfo = @{@"hello" : @"how are you", @"msg" : @"success"}; // 2.执行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];

这里要说一点,就是iOS系统限制了注册本地推送的数量,最大的注册量为64条。

接收推送

  • 应用在前台或后台,未被杀死时。
//程序处于前台或后台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"333这里被调用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序在前台或后台,未被杀死,点击通知栏调用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
}
  • 程序已被杀死时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
//添加处理代码 NSLog(@"666这里被调用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序已被杀死,点击通知栏调用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
} return YES;
}

iOS 10之后

先导入这个东西#import <UserNotifications/UserNotifications.h>

注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
center.delegate = self; //iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}]; return YES;
}

发送通知

// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"本地推送Title" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"本地推送Body" arguments:nil];
content.sound = [UNNotificationSound defaultSound]; // 在 设定时间 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger]; //添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];

通知处理

实现UNUserNotificationCenterDelegate代理方法:

  • 第一个方法:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

    // 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
completionHandler(UNNotificationPresentationOptionSound);
}

这个方法中的那句话就是,当应用在前台的时候,收到本地通知,是用什么方式来展现。系统给了三种形式:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
  • 第二个代理方法:

这个方法是在后台或者程序被杀死的时候,点击通知栏调用的,在前台的时候不会被调用

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show]; completionHandler();
}

iOS 本地消息推送机制的更多相关文章

  1. IOS - 本地消息推送

    第一步:创建本地推送 // 创建一个本地推送 UILocalNotification *notification = [[[UILocalNotification alloc] init] autor ...

  2. (转)iOS消息推送机制的实现

    原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...

  3. ios消息推送机制原理与实现

    本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...

  4. Android本地消息推送

    项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...

  5. iOS 10 消息推送(UserNotifications)秘籍总结(二)

    背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...

  6. APP消息推送机制的实现(PUSH)

    出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...

  7. MVC异步消息推送机制

    在MVC里面,有异步控制器,可以实现模拟消息推送机制功能 1.控制器要继承至AsyncController,如 public class RealTimeController : AsyncContr ...

  8. Android (Notification)消息推送机制

    从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...

  9. 【iOS】iOS消息推送机制的实现

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

随机推荐

  1. 宋牧春: Linux设备树文件结构与解析深度分析(1) 【转】

    转自:https://mp.weixin.qq.com/s/OX-aXd5MYlE_YoZ3p32qWA 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...

  2. 认识Cookie和状态管理

    HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的 即使 HTTP1.1 支持持续连接,但当用户有一段时间没有提交请求,连接也会 ...

  3. 华东师范大学第十届ECNU Coder程序设计竞赛

    华东师范大学第十届ECNU Coder程序设计竞赛 浮点数模运算 solution 转成整数然后取模. 时间复杂度:\(O(1)\) 数螃蟹 solution 找出公差出现次数最多的作为公差,然后找出 ...

  4. css预处理scss环境配置

    css 预处理器 CSS 预处理器用一种专门的编程语言,进行 Web css编码,然后再编译成正常的 CSS 文件,以供项目使用:说简单点就是在某个环境下写css 可以写变量.表达式.嵌套等,在通过该 ...

  5. java程序改错题(常见)

    最近跑校招,做了一套java的笔试题. abstract class Name { private String name; public abstract boolean isStupidName( ...

  6. What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)

    I go to the gym and lift weights because I like the feeling of getting stronger and better. Two mont ...

  7. ssh -o 常用选项

    ssh -o ConnectTimeout=3 -o ConnectionAttempts=5 -o PasswordAuthentication=no -o StrictHostKeyCheckin ...

  8. vue数据传递--我有特殊的实现技巧

    最近碰到了比较多的关于vue的eventBus的问题,之前定技术选型的时候也被问到了,vuex和eventBus的使用范围.所以简单的写一下.同时有一种特殊的实现方案. 有这么几种数据传递方式,vue ...

  9. ASP.NET Core 2.0 MVC 发布部署--------- linux安装.NET CORE SDK具体操作链接以及操作总细节

    具体链接:https://www.microsoft.com/net/learn/get-started/linuxubuntu 如下图:

  10. asp基础

    0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...