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的缩写,是苹果的服务 ...
随机推荐
- django框架<二>
django框架: Models 1.基本创建 Django提供了一个抽象层("Model")的构建和管理Web应用程序的数据. Django使用一种新的方式,即:关系对象映射 ...
- uWSGI+Nginx+Flask在Linux下的部署
搞了一天多,终于搞通了uWSGI的部署原理,下面总结一下遇到的一些坑,希望给读者能够少走弯路. 简单来说,uWSGI是一个web服务器,Nginx进行反向代理的其实跟这些服务器可以说没有 ...
- MongoDB 3.x 安装配置
目录 (见右侧目录栏导航)- 1. 安装Mongodb - 1.1 使用二进制包安装 - 1.2 运行MongoDB- 2. MongoDB 配置文件详解 - 2.1 说明 - ...
- 百度2017春招<度度熊回家问题>
题目: 一个数轴上共有N个点,第一个点的坐标是度度熊现在位置,第N-1个点是度度熊的家.现在他需要依次的从0号坐标走到N-1号坐标.但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个 ...
- ntp 控制报文
//make the procedure into block//2014.7.23 OK//#include "CSocket.h" #define NTP_SERVER_IP ...
- beego学习笔记(4):开发文档阅读(5)
controller的逻辑: 我们看下面的代码,就知道怎么传值的: import ( "github.com/astaxie/beego" ) type MainControlle ...
- csu 1806 & csu 1742 (simpson公式+最短路)
1806: Toll Time Limit: 5 Sec Memory Limit: 128 MB Special JudgeSubmit: 256 Solved: 74[Submit][Sta ...
- 高屋建瓴之WebMail攻与防
0x01:前言 随着互联网的快速发展,我们的生活与互联网的联系愈加的紧密.各种快捷方便的信息化通信工具渐渐取代了传统的通信方式.微博.QQ.MSN.微信.陌陌, …这样的社交软件和平台已经成为了我们生 ...
- [转]Java中堆和栈创建对象的区别
转载自http://blog.csdn.net/hbhhww/article/details/8152838 栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序 ...
- Source Insight 4.0 文件类型、编码格式、tab转空格、tab键自动补全设置。。。
1.编码格式 -- 在 Options->Preferences->Files 中的最下面,Default enconding 为 UTF-8 2.tab转空格 其他相关设置如下: 以下 ...