一、推送通知

注意:这里说的推送通知跟NSNotification有所区别
NSNotification是抽象的,不可见的
推送通知是可见的(能用肉眼看到)
 
iOS中提供了2种推送通知
本地推送通知(Local Notification)
远程推送通知(Remote Notification)
 
二、本地推送通知
1.什么是本地推送通知
顾名思义,就是不需要联网就能发出的推送通知(不需要服务器的支持)
 
2.本地推送通知的使用场景
常用来定时提醒用户完成一些任务,比如
清理垃圾、记账、买衣服、看电影、玩游戏
 
3.如何发出本地推送通知
创建本地推送通知对象

UILocalNotification *ln = [[UILocalNotification alloc] init];

设置本地推送通知属性
推送通知的触发时间(何时发出推送通知)

@property(nonatomic,copy) NSDate *fireDate;

推送通知的具体内容

@property(nonatomic,copy) NSString *alertBody;

在锁屏时显示的动作标题(完整标题:“滑动来” + alertAction)

@property(nonatomic,copy) NSString *alertAction;

音效文件名

@property(nonatomic,copy) NSString *soundName;

app图标数字

@property(nonatomic) NSInteger applicationIconBadgeNumber;

调度本地推送通知(调度完毕后,推送通知会在特地时间fireDate发出)

[[UIApplication sharedApplication] scheduleLocalNotification:ln];

获得被调度(定制)的所有本地推送通知

@property(nonatomic,copy) NSArray *scheduledLocalNotifications;

(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)

取消调度本地推送通知

- (void)cancelLocalNotification:(UILocalNotification *)notification;

- (void)cancelAllLocalNotifications;

立即发出本地推送通知

- (void)presentLocalNotificationNow:(UILocalNotification *)notification;

每隔多久重复发一次推送通知

@property(nonatomic) NSCalendarUnit repeatInterval;

点击推送通知打开app时显示的启动图片

@property(nonatomic,copy) NSString *alertLaunchImage;

附加的额外信息

@property(nonatomic,copy) NSDictionary *userInfo;

时区

@property(nonatomic,copy) NSTimeZone *timeZone;

(一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)

4.点击本地推送通知

当用户点击本地推送通知,会自动打开app,这里有2种情况
app并没有关闭,一直隐藏在后台
让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

 
app已经被关闭(进程已死)
启动app,启动完毕会调用AppDelegate的下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

²launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象
 
 

三、远程推送通知

1.什么是远程推送通知
顾名思义,就是从远程服务器推送给客户端的通知(需要联网)
远程推送服务,又称为APNs(Apple Push Notification Services)
2.为什么需要远程推送通知?
传统获取数据的局限性
只要用户关闭了app,就无法跟app的服务器沟通,无法从服务器上获得最新的数据内容
3.远程推送通知可以解决以上问题
不管用户打开还是关闭app,只要联网了,都能接收到服务器推送的远程通知
 
4.远程推送通知使用须知
所有的苹果设备,在联网状态下,都会与苹果的服务器建立长连接
什么是长连接
只要联网了,就一直建立连接
长连接的作用
时间校准
系统升级
查找我的iPhone
.. ...
长连接的好处
数据传输速度快
数据保持最新状态
 
5.属性
客户端如果想接收APNs的远程推送通知,必须先注册(得到用户的授权)
一般在App启动完毕后就马上注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// 注册远程通知

UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;

[application registerForRemoteNotificationTypes:type];

return YES;

}

如果是第一次注册,会弹出右边的对话框
 
注册成功后会调用AppDelegate的下面方法,得到设备的deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

NSLog(@"%@", deviceToken);

}

当用户点击远程推送通知,会自动打开app,这里有2种情况
app并没有关闭,一直隐藏在后台
让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

 
app已经被关闭(进程已死)
启动app,启动完毕会调用AppDelegate的下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

²launchOptions参数通过UIApplicationLaunchOptionsRemoteNotificationKey取出服务器返回的字典内容
 
 
6.从获得deviceToken 到 推送消息给设备 的过程
 

一.开发iOS程序的推送功能, iOS端需要做的事

1.请求苹果获得deviceToken

2.得到苹果返回的deviceToken

3.发送deviceToken给公司的服务器

4.监听用户对通知的点击

二.调试iOS的远程推送功能, 必备条件:

1.真机

2.调试推送需要的证书文件

1> aps_development.cer : 某台电脑就能调试某个app的推送服务

2> ios_development.cer : 让电脑具备真机调试的能力(调试设备)

3> iphone5_qq.mobileprovision : 某台电脑就能利用某台设备调试某个程序

三.发布具有推送服务的app

1> aps_production.cer : 如果发布的程序中包含了推送服务,就必须安装这个证书

2> ios_distribution.cer  : 让电脑具备发布程序的能力

3> qq.mobileprovision  : 某台电脑就能发布某个程序

远程推送

 //
// AppDelegate.m
// 01-获取DeviceToken
//
// Created by apple on 14/11/10.
// Copyright (c) 2014年 heima. All rights reserved.
// #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. if ([UIDevice currentDevice].systemVersion.doubleValue <= 8.0) {
// 不是iOS8
UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
// 当用户第一次启动程序时就获取deviceToke
// 该方法在iOS8以及过期了
// 只要调用该方法, 系统就会自动发送UDID和当前程序的Bunle ID到苹果的APNs服务器
[application registerForRemoteNotificationTypes:type];
}else
{
// iOS8
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
// 注册通知类型
[application registerUserNotificationSettings:settings]; // 申请试用通知
[application registerForRemoteNotifications];
} // 1.取出数据
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) {
static int count = ;
count++;
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.numberOfLines = ;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:];
label.backgroundColor = [UIColor orangeColor];
label.text = [NSString stringWithFormat:@" %@ \n %d", userInfo, count];
[self.window.rootViewController.view addSubview:label];
} return YES;
} /**
* 获取到用户对应当前应用程序的deviceToken时就会调用
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken);
// <47e58207 31340f18 ed83ba54 f999641a 3d68bc7b f3e2db29 953188ec 7d0cecfb>
// <286c3bde 0bd3b122 68be655f 25ed2702 38e31cec 9d54da9f 1c62325a 93be801e>
} /*
ios7以前苹果支持多任务, iOS7以前的多任务是假的多任务
而iOS7开始苹果才真正的推出了多任务
*/
// 接收到远程服务器推送过来的内容就会调用
// 注意: 只有应用程序是打开状态(前台/后台), 才会调用该方法
/// 如果应用程序是关闭状态会调用didFinishLaunchingWithOptions
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/*
如果应用程序在后台 , 只有用户点击了通知之后才会调用
如果应用程序在前台, 会直接调用该方法
即便应用程序关闭也可以接收到远程通知
*/
NSLog(@"%@", userInfo); // static int count = 0;
// count++;
// UILabel *label = [[UILabel alloc] init];
// label.frame = CGRectMake(0, 250, 200, 200);
// label.numberOfLines = 0;
// label.textColor = [UIColor whiteColor];
// label.text = [NSString stringWithFormat:@"%@ \n %d", userInfo, count];
// label.font = [UIFont systemFontOfSize:11];
// label.backgroundColor = [UIColor grayColor];
// [self.window.rootViewController.view addSubview:label];
} //接收到远程服务器推送过来的内容就会调用
// ios7以后用这个处理后台任务接收到得远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
/*
UIBackgroundFetchResultNewData, 成功接收到数据
UIBackgroundFetchResultNoData, 没有;接收到数据
UIBackgroundFetchResultFailed 接收失败 */
// NSLog(@"%s",__func__);
// NSLog(@"%@", userInfo); NSNumber *contentid = userInfo[@"content-id"];
if (contentid) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.numberOfLines = ;
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"%@", contentid];
label.font = [UIFont systemFontOfSize:];
label.backgroundColor = [UIColor grayColor];
[self.window.rootViewController.view addSubview:label];
//注意: 在此方法中一定要调用这个调用block, 告诉系统是否处理成功.
// 以便于系统在后台更新UI等操作
completionHandler(UIBackgroundFetchResultNewData);
}else
{
completionHandler(UIBackgroundFetchResultFailed);
} }
@end

本地推送

 //
// MJViewController.m
// 06-本地通知
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "MJViewController.h" @interface MJViewController ()
- (IBAction)addLocalNote;
- (IBAction)cancelLocalNote; @end @implementation MJViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)addLocalNote {
// 1.创建通知
UILocalNotification *localNote = [[UILocalNotification alloc] init]; // 2.设置属性
localNote.alertAction = @"开始玩游戏"; // 操作标题
localNote.alertBody = @"都好几天了, 你赶紧用一下我吧!!!"; // 正文
localNote.applicationIconBadgeNumber = ;
// localNote.repeatInterval = NSCalendarUnitMinute;
localNote.alertLaunchImage = @"Default"; // 点击通知, 打开程序时候现实的启动图片
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
// 3.注册通知(添加)
UIApplication *app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
[app scheduleLocalNotification:localNote];
} - (IBAction)cancelLocalNote {
UIApplication *app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
}
@end
 //
// MJAppDelegate.m
// 06-本地通知
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "MJAppDelegate.h" @interface MJAppDelegate()
//@property (nonatomic, weak) UILabel *label;
@end @implementation MJAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", launchOptions];
[self.window.rootViewController.view addSubview:label];
// self.label = label; // Override point for customization after application launch. NSLog(@"didFinishLaunchingWithOptions---");
return YES;
} /**
说明用户点击通知, 进入了程序(程序还在运行中, 程序并没有被关掉)
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.backgroundColor = [UIColor blueColor];
label.text = @"didReceiveLocalNotification";
[self.window.rootViewController.view addSubview:label]; NSLog(@"didReceiveLocalNotification");
} - (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end

IOS-推送通知的更多相关文章

  1. “iOS 推送通知”详解:从创建到设置到运行

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  2. iOS推送通知

    推送通知 此通知非彼通知. NSNotification是抽象的,看不见的,但是可以监听,属于观察者模式的一种设计模式. 推送通知是可见的,能用肉眼看见的,是真正的和用户打交道的通知. 推送通知分为两 ...

  3. iOS推送通知的实现步骤

    一.关于推送通知 来源:http://blog.csdn.net/enuola/article/details/8627283 推送通知,也被叫做远程通知,是在iOS 3.0以后被引入的功能.是当程序 ...

  4. 【PHP】iOS推送通知以及反馈服务

    近来项目是完成一个PHP的推送服务器,无论是PHP,APNs还是GCM基本上都是从零开始. 写下一点见解,方便以后继续做代码的搬运工. 因为对PHP跟iOS都不熟悉,可能有错漏...穷孩子没有用过iO ...

  5. iOS推送通知流程

    ①注册推送通知使用方法:registerUserNotificationSettings, registerForRemoteNotifications ④APP发送deviceToken到第三方: ...

  6. Clojure:两步发送iOS推送通知(apns)

    首先在project.clj中,添加对notnoop 类库的引用:[com.notnoop.apns/apns "0.2.3"] 然后使用如下方法就可以发送推送消息了: (ns d ...

  7. iOS 推送通知处理

    //这是程序杀死后再通过点击通知进入时调用的方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...

  8. IOS推送通知測试工具PushMeBaby

    下载了PushMeBaby在xcode5里中不能使用.类库变了.须要加入Carbon.framework库.在引用的地方改成: #include <Carbon/Carbon.h>.程序就 ...

  9. 转:向IOS设备发送推送通知

    背景 SMS 和 MMS 消息是由无线运营商通过设备的电话号码向特定设备提供的.实现 SMS/MMS 的服务器端应用程序的开发人员必须费大量精力才能与现有的封闭电信基础架构进行交互(其中包括获取电话号 ...

  10. iOS推送 再备

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

随机推荐

  1. quartz集群 定时任务 改成可配置

    前面的博文中提到的quartz集群方式会有以下缺点: 1.假设配置了3个定时任务,job1,job2,job3,这时数据库里会有3条job相关的记录,如果下次上线要停掉一个定时任务job1,那即使定时 ...

  2. xshell上传下载文件

    上传文件到服务器rz 从服务器下载文件sz

  3. numpy.random.random & numpy.ndarray.astype & numpy.arange

    今天看到这样一句代码: xb = np.random.random((nb, d)).astype('float32') #创建一个二维随机数矩阵(nb行d列) xb[:, 0] += np.aran ...

  4. shell中的for、while、until(二)

    1.C语言格式的for命令: for((var; condition;iteration process)) 注意: 1.给变量赋值可以有空格 2.条件中的变量不以美元符开头: 3.迭代过程的算式未用 ...

  5. jmeter+ant+jenkins接口自动环境搭建

    ant 下载地址:http://archive.apache.org/dist/ant/binaries/ 下载:apache-ant-1.9.7-bin.zip 解压到系统盘下:D:\apache- ...

  6. 虚拟环境virtualenv和virtualenvwrapper(转)

    virtualenv是用来创建一个独立的Python虚拟环境的工具,通过virtualenv可以创建一个拥有独立的python版本和安装库的虚拟开发环境.这样一来我们就可以在虚拟环境中安装各种各种所需 ...

  7. Java集合(3):Vector && Stack

    一.Vector介绍 Vector可以实现可增长的动态对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.不过,Vector的大小是可以增加或者减小的,以便适应创建Vector后进行添加或者删 ...

  8. org.springframework-jdbc

    Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate 博客分类: spring JDBCSpringSQL编程数据结构  今天看了下Spr ...

  9. 初学hadoop的个人历程

       在学习hadoop之前,我就明确了要致力于大数据行业,成为优秀的大数据研发工程师的目标,有了大目标之后要分几步走,然后每一步不断细分,采用大事化小的方法去学习hadoop.下面开始叙述我是如何初 ...

  10. 正则表达式和python的re模块

    0 正则表达式 0.1 常见的元字符 .:    匹配除\r\n之外的任何单个字符 *:    匹配前面的子表达式任意次,例如Zz*可以匹配Z,可以匹配Zz,也可以匹配Zzzzzzzzzz +:    ...