iOS 本地消息推送机制
转发自: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 本地消息推送机制的更多相关文章
- IOS - 本地消息推送
第一步:创建本地推送 // 创建一个本地推送 UILocalNotification *notification = [[[UILocalNotification alloc] init] autor ...
- (转)iOS消息推送机制的实现
原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...
- ios消息推送机制原理与实现
本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...
- Android本地消息推送
项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...
- APP消息推送机制的实现(PUSH)
出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...
- MVC异步消息推送机制
在MVC里面,有异步控制器,可以实现模拟消息推送机制功能 1.控制器要继承至AsyncController,如 public class RealTimeController : AsyncContr ...
- Android (Notification)消息推送机制
从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...
- 【iOS】iOS消息推送机制的实现
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...
随机推荐
- rebbitmq之python_pika监控远程连接及自动恢复(七)
前言 客户端连接rabbitmq后,如果长时间没有数据的传输,rabbitmq会申请关闭TCP连接,造成该TCP连接下的所有的信道都不可用,很多时候为了传输数据的高效率,我们会先创建一个信道池,这样省 ...
- Linux 用户态与内核态的交互【转载】
Linux 用户态与内核态的交互 在 Linux 2.4 版以后版本的内核中,几乎全部的中断过程与用户态进程的通信都是使用 netlink 套接字实现的,例如iprote2网络管理工具,它与内核的交 ...
- 中高级JAVA面试知识点(个人整理)
JVM运行时数据区域 方法区: 用 于存储虚拟机加载的类信息,常量,静态变量,JIT编译后的代码,所有线程共享 堆:所有线程共享,用来存储实例对象. 虚拟机栈:线程私有,生命周期与线程相同,每个方法被 ...
- NLP基础 成分句法分析和依存句法分析
正则匹配: .除换行符所有的 ?表示0次或者1次 *表示0次或者n次 a(bc)+表示bc至少出现1次 ^x.*g$表示字符串以x开头,g结束 |或者 http://regexr.com/ 依存句法分 ...
- ifconfig与内核通信 ifreq 结构体分析和使用
结构原型: /* * Interface request structure used for socket * ioctl's. All interface ioctl's must have p ...
- spring源码分析---事务篇
上一篇我介绍了spring事务的传播特性和隔离级别,以及事务定义的先关接口和类的关系.我们知晓了用TransactionTemplate(或者直接用底层P的latformTransactionMana ...
- Spring Boot Admin Quick Start
Quick Start 1. Spring Boot Admin是干什么的? 用来监控Spring Boot项目,可以通过Spring Boot Admin Client(via Http) 或者 使 ...
- Node.js fs-文件系统
fs.stat,获取文件信息. var fs = require('fs') fs.stat('../index.js', (err, stats) => { if (err) { consol ...
- booklist for machine learning
Recommended Books Here is a list of books which I have read and feel it is worth recommending to fri ...
- vscode vue配置和一些其它辅助【工具篇】
后续有补充就经常更新