Apple Push Notification service

  APNS是iOS的消息推送机制,网上有很好的资料,请查看最后的参考列表,这里强调一些关键步骤。

一、生成证书。

  与苹果的APNS Server交互涉及到iOS设备和提供Push服务的Provider,它们均需要证书进行验证。iOS设备与APNS的交互是通过底层进行的,应用程序本身并不需要引用什么证书,但是你的应用是否能够成功注册消息通知,会与你项目中的Bundle identifier有关,你指定的Bundle identifier所属的Provisioning Profile下的AppID必须开通了"Enable for Apple Push Notification service",如下图:

从图可见,分开了Development和Production,生成Provider证书时,如果是开发环境生成Development的证书,如果是生产环境生成Production的证书。

Bundle identifier --> Provision Profile --> AppID --> Enable for Apple Push Notification service --> Push SSL Certificate

可见,虽然iOS应用并不需要加载下载下来的证书,但实际上它也是存在与其对应的证书的,只是通过Bundle identifier标识。

注意,如果之前没有为对应的AppID开通APNS,此时就需要下载新的Provision Profile并重新安装,重装Profile的方法很简单,打开Organizer窗口,删除对应的Provision Profile文件,然后把新下载的文件拖拉到窗口中就OK了。

  如果要在应用程序中调试通过还需要做些配置,否则会出现如下错误:

“no valid aps-environment entitlement found for application”

我们打开刚才下载下来的新Provision Profile文件,其中有一段配置:

    <key>Entitlements</key>    <dict>        <key>application-identifier</key>        <string>7C57XDWF8L.com.xxx.appname</string>        <key>aps-environment</key>        <string>development</string>        <key>get-task-allow</key>        <true/>        <key>keychain-access-groups</key>        <array>            <string>7C57XDWF8L.*</string>        </array>    </dict>

我们需要在iOS项目中添加Entitlements配置,步骤如下:

1、开启并添加配置。

.xcodeproj --> Summary --> Entitlements节 --> 选中"Enable Entitlements"

些时会自动生成一个${PRODUCT_NAME}.entitlements的文件,在文件中添加键值对:

Key   Type   Value  
get-task-allow   Bollean   YES  
aps-environment   String   development   这行是必须的
application-identifier   String   7C57XDWxxx.com.xxx.appname  
keychain-access-groups   Array

  Item0 String 7C57XDWxxx.*

 

2、也可以指定使用Entitlements.entitlements文件,通过以下方法添加文件:

New Files --> Code Siging --> Entitlements --> Next...

文件的内容和上述相同,在配置中选择此文件即可。

3、订阅和退订。

iOS设备对APNS进行订阅或退订消息通知的行为,可以在*AppDelegate.m中的application:didFinishLaunchingWithOptions:方法进行,也可以在别的*ViewController.m的某个触发事件中进行。

订阅:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge  | 
  UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

退订:

 [[UIApplication sharedApplication] unregisterForRemoteNotifications];

如果想要避免每次启动都执行订阅和接收DeviceToken的操作,可以进行如下判断:

if([[UIApplication sharedApplication] enabledRemoteNotificationTypes]{   [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge  | 
    UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];}

这样可能只会在第一次安装程序的时候才会订阅并接收到DeviceToken,如果后来调用了unregisterForRemoteNotifications,也不会再次执行了。

4、Delegate方法。

当调用registerForRemoteNotifications方法,或者应用程序接收到来源自于APNS的通知时都会触发相关的delegate方法,因为这些delegate方法定义在UIApplicationDelegate中,所以实现需要做在*AppDelegate.m类中。例如:

//注册push服务- (void)application:(UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:    [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; //去掉"<>"
    token = [[token description] stringByReplacingOccurrencesOfString:@" " withString:@""];//去掉中间空格    NSLog(@"deviceToken: %@", token);}

//注册push服务失败- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {    NSLog(@"Error in registration for APNS. Error: %@", error);}

//接收到push消息- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    NSLog(@"收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"                                                     message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]                                                   delegate:self                                          cancelButtonTitle:@"关闭"                                          otherButtonTitles:@"更新状态",nil];    [alert show];    [alert release];}
注意:通过 application:didRegisterForRemoteNotificationsWithDeviceToken:获取到DeviceToken后需要推送给提供Push服务的Provider,Provider保存好此应用的uuid对应的token,以便在发送消息的时候使用。如何生成.NET所需证书的可以根据这里的步骤来做。二、Provider开发。可以通过apns-sharp这个开源库进行开发,另外还有很多相关的文章详细解说其开发原理,例如这几篇文章:Local and Push Notification Programming Guide: About Local Notifications and Push NotificationsLocal and Push Notification Programming Guide: Provider Communication with Apple Push Notification ServiceProgramming Apple Push Notification Services Apple Push Notification Service另外,还有PushMeBaby供XCode下开发测试的Provider。轉自:http://www.cnblogs.com/chenjunbiao/archive/2011/07/28/2119259.html

Apple Push Notification service相关推荐

  1. Apple Push Notification Service(苹果推送服务)

    https://developer.apple.com/library/IOS/documentation/NetworkingInternet/Conceptual/RemoteNotificati ...

  2. [转]宝文!Apple Push Notification Service (APNS)原理与实现方案

    原理 简单的说,app要单独实现消息动态更新,一种是轮询,这对用户来说会带来额外的流量.另一种方案是push,app client和server直接保持一个长连接,有新的消息时server push给 ...

  3. ×××送通知服务教程 Apple Push Notification Services Tutorial

    本文译自http://www.raywenderlich.com/.原文由iOS教程团队 Matthijs Hollemans 撰写,经原网站管理员授权本博翻译. 在iOS系统,考虑到手机电池电量,应 ...

  4. APNS(Apple Push Notification services)

    苹果的推送服务APNs基本原理简单来说就是苹果利用自己专门的推送服务器(APNs)接收来自我们自己应用服务器的需要被推送的信息,然后推送到指定的iOS设备上,然后由设备通知到我们的应用程序,设备以通知 ...

  5. Apple PUSH Notication Service (APNS) 配置攻略

    iOs 3.0以后就支持APNS( apple push notication Service).下面介绍怎么配置APNS服务.APNS 分为客户端与服务器端2个部分:客户端部分:1 创建一个App ...

  6. Xcode The 'Apple Push Notification' feature is only available to users enrolled in Apple Develo.

    如果在调试APP的时候出现这样的提示 The 'Apple Push Notification' feature is only available to users enrolled in Appl ...

  7. Xcode The 'Apple Push Notification' feature is only available to users enrolled in Apple Develo...

    一个带有远程通知的项目用其他的开发者账号真机运行Xcode 出现这样的问题: The 'Apple Push Notification' feature is only available to us ...

  8. 怎样编写Apple Push Notification服务器

    http://www.iphone-geek.cn/%E7%BC%96%E7%A8%8B/%E6%80%8E%E6%A0%B7%E7%BC%96%E5%86%99apple-push-notifica ...

  9. 为Apple Push开发的PHP PEAR 包:Services_Apple_PushNotification

    Apple Push Notification Service:通过苹果服务器向app用户推送消息,无需启动app. 苹果官方文档:http://developer.apple.com/library ...

  10. IOS提交审核 错误Missing Push Notification Entitlement

    Xcode上传IPA被退回的错误: Missing Push Notification Entitlement - Your app appears to register with the Appl ...

最新文章

  1. 心灵小栈: 镌刻在地下500米的母爱
  2. 去掉[]中的英文(正则表达式)C#
  3. 2021年春季学期-信号与系统-第六次作业参考答案
  4. MDX学习笔记(整理) MDX语法
  5. 数据库系统概论:第二章 关系数据库
  6. Python基础教程:内置类型之生成器
  7. 贝叶斯决策思想的应用与延伸
  8. python实现文本编辑器_Python-tkinter实现简单的文本编辑器
  9. ASP.NET Core 中间件
  10. react如何卸载组件_18道 React 面试必考题含解答面试高频
  11. 面试中被问到HashMap的结构,1.7和1.8有哪些区别?这篇做深入分析!
  12. 什么时候不选择mysql_MySQL请选择合适的列_MySQL
  13. 抗腹泻药行业调研报告 - 市场现状分析与发展前景预测
  14. appium 原理解析
  15. 黄聪:JQUERY的datatables插件,Date range filter时间段筛选功能
  16. matlab仿真的五个步骤,matlab仿真步骤
  17. linux系统及编程基础唐晓君,Linux-Shell编程之判断文件类型
  18. [996]如何申请高德地图用户Key
  19. dede 后台 一直显示验证码不正确的原因
  20. 通许县中等职业学校计算机,2021通许县中等职业学校招生简章

热门文章

  1. GDELT数据库入门与了解(码字中...)
  2. 微信小程序JavaScript语法介绍
  3. 涂料价格上涨原因剖析
  4. 关闭windows电脑 ctrl +alt +方向键旋转屏幕快捷键
  5. Python学习笔记-基础篇
  6. 数据结构(一)、二叉树(BT),二叉查找树(BST),平衡二叉树(AVL树)
  7. 【MyBatis基础】(09)- 逆向工程(代码自动生成)
  8. 什么叫做云计算?云计算基础学习路线
  9. HTML的font标签的使用
  10. Mybatis-Plus报错:Invalid bound statement (not found)