APNS(Apple Push Notification Services)苹果专门的推送服务器 接收咱们本身应用服务器须要被推送的消息 而后推送到咱们的手机 手机通知咱们的应用程序 php
注册的大概流程:html
1 设备须要向APNS服务器注册ios
2 注册成功后返回device_token值数组
3 将这个token值发送给咱们本身的服务器 服务器
4 有须要推送的消息时 本身的服务器将消息按必定的格式打包 结合token值发送给APNS服务器 网络
5 因为App与APNS维持一个基于TCP的长链接 APNS将消息推送到咱们的手机上app
推送大体流程学习
1 你的APP支持推送 APP链接网络后会链接APNS 链接过程当中 APNS会验证devices_token 链接成功后维持一个长链接测试
2 咱们的服务器接收到须要推送的消息后将消息和设备的device_token一块儿打包发送给APNS服务器url
3 APNS服务器接收到消息发送到指定device_token设备
4 设备接收到消息 通知咱们的App
打包的消息结构
观看大神写的详情:http://www.cnblogs.com/taintain1984/p/3723324.html
1 本地推送
应用本地提醒用户事件
// 注册本地推送消息
+ (void)registerLocalNotification:(NSInteger)alertTime {
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 设置触发通知的时间
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
// 时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复的间隔
notification.repeatInterval = kCFCalendarUnitSecond;
// 通知内容
notification.alertBody = @"该起床了...";
notification.applicationIconBadgeNumber = 1;
// 通知被触发时播放的声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知参数
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
notification.userInfo = userDict;
// ios8后,须要添加这个注册,才能获得受权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,能够是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重复提示的单位,能够是天、周、月
notification.repeatInterval = NSCalendarUnitMonth;
}
// 执行通知注册
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
// 接收到推送消息执行的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"noti:%@",notification);
// 这里真实须要处理交互的地方
// 获取通知所带的数据
NSString *notMess = [notification.userInfo objectForKey:@"key"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"message:notMess delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
// 更新显示的徽章个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
}
// 在Appdelegate 中调用注册推送消息的方法
[AppDelegate registerLocalNotification:10];
想要取消推送 须要调用
// 取消某个本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
// 获取全部本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
// 若是找到须要取消的通知,则取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}
大神博客介绍:http://blog.csdn.net/woaifen3344/article/details/44302635
2 服务器推送
根据APP ID建立推送证书 转化成p12 再转成pem给后台用 后台推送的时候要把这个证书和push.php放在一块儿 后台使用push.php放在一块儿 后台使用push.php推送的时候要读取这个证书
建立一个测试证书
Appdelegate注册推送服务 而且经过代理方法的回调来获取注册成功的token值或者注册失败的信息(注册须要设置联网才能够 token能够理解为用户手机的惟一标识和应用程序的App ID组合生成的一个惟一标识,用来标识是哪一个手机上的App token不是一直不变的 好比测试和发布的程序返回的token是不同的)
把token值给服务器 这样服务器就会接受每个赞成了使用推送服务的用户的token 能够保存为一张表 当后台服务器须要发送消息的时候就能够从这张表里面找token后去发送消息
if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0) {
// 1 向用户发送请求 去注册推送服务
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; //弹框 角标 声音
// 2 把推送设置配置给咱们的应用程序
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];//iOS8.0
// 3 注册推送服务
[[UIApplication sharedApplication] registerForRemoteNotifications];
}else{
// iOS8.0前的方法
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// 当前应用程序正在运行时候 接收到推送消息会执行的这个方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
_commentLabel.text = [userInfo objectForKey:@"aps"][@"alert"];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"注册远程推送服务失败:%@",error);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"token :%@",deviceToken);
// 正常状况下 会在这个地方发送请求 把这个token返回给本身的服务器
}
// 请求本地服务器数据
NSString *urlStr=[NSString stringWithFormat:@"http://10.80.6.165/push.php?deviceName=%@&message=%@",_message,_messageTF.text];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"ERROR:%@",connectionError);
}else{
NSLog(@"请求成功");
}
}];
//详情介绍: http://blog.csdn.net/shenjie12345678/article/details/41120637