1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// other codes here.
return YES;
} - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken: %@", deviceToken);
} - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error in registration. Error: %@", error);
} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{ NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@" 关闭"
otherButtonTitles:@" 更新状态",nil];
[alert show];
[alert release];
}
}

启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:

  1. deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>

2. 生成app在服务端需要的许可证
1)进入Provisioning Portal, 下载Certificates在development下的证书。 
2) 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3) 双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key各自export出一个.p12文件。(会出现设置密码过程)
4)需要将上面的2个.p12文件转成.pem格式:

  1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
  1. openssl pkcs12 -nocerts -out key.pem -in key.p12

5)如果需要对 key不进行加密:

  1. openssl rsa -in key.pem -out key.unencrypted.pem

6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。

  1. cat cert.pem key.unencrypted.pem > ck.pem

3. 服务端push通知到ANPS. 在cocoachina找到了两种方法:
  1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:

  1.  $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
    $pass = '123456'; // Passphrase for the private key (ck.pem file) // Get the parameters from http get or from command line
    $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
    $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
    $sound = $_GET['sound'] or $sound = $argv[3]; // Construct the notification payload
    $body = array();
    $body['aps'] = array('alert' => $message);
    if ($badge)
    $body['aps']['badge'] = $badge;
    if ($sound)
    $body['aps']['sound'] = $sound; /* End of Configurable Items */
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    // assume the private key passphase was removed.
    stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); // connect to apns
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$fp) {
    print "Failed to connect $err $errstr\n";
    return;
    }
    else {
    print "Connection OK\n
    ";
    } // send message
    $payload = json_encode($body);
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
    print "Sending message :" . $payload . "\n";
    fwrite($fp, $msg);
    fclose($fp);
    ?>

请 求一次 http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:

  1. Connection OK
  2. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}

本文作者为转自 CocoaChina会员“marshluca”,帖子地址 http://www.cocoachina.com/bbs/read.php?tid-20723.html

2)pushMeBaby 驱动。将aps_developer_identity.cer导入到project里面,改名为apns.cer。

 
 

APNS 服务推送通知的更多相关文章

  1. APNS 远程推送通知 PUSH deviceToken

    服务器向客户端推送消息:      当应用程序推到后台,或者根本就没有运行(我们的代码无能为力)      如果这种情况之下,应用程序想和用户交互(传统的做法 不可能)      推送 APNS:Ap ...

  2. 苹果推送通知服务APNs编程(转)

    add by zhj: 下面的几篇文章也非常好, http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios- ...

  3. .NET向APNS苹果消息推送通知

    一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...

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

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

  5. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  6. wp8.1 Study16:网络之 使用Azure移动服务及利用Azure推送通知服务

    一.WP8.1有关网络的API WP8.1与其它平台的对比如下图: 二.Azure移动服务 前提: Azure移动服务可以让使用者的数据存放在云空间,从而方便使用者的App在不同平台上的数据共享. 1 ...

  7. iOS 点击Application icon加载推送通知Data

    今天做APNS远程推送通知遇到了一个问题,就是手机接收到通知的时候,如果马上点击通知的 alert view时候,系统马上唤醒你的Application,通知或调用你的didReceiveLocalN ...

  8. (转)苹果推送通知服务教程 Apple Push Notification Services Tutorial

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

  9. WP7推送通知服务

    原文地址http://www.cnblogs.com/Joetao/articles/2214482.html (一)为什么使用推送通知服务(1)Windows Phone执行模型决定只有一个第三方的 ...

随机推荐

  1. Tomcat编码配置解疑

    环境:tomcat6.0.23 jdk 1.6 相关参考: http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html http://wiki.ap ...

  2. ie浏览器 jsp中链接参数为中文的处理

    在js中,使用 encodeURIComponent(encodeURIComponent("警情抽查"))来处理参数,例如: mini.get("khxmdm" ...

  3. 用Join子句进行分组联接

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 论文笔记

    StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks  本文将利 ...

  5. EHcache缓存框架详解

    EhCache是一个纯Java的进程内缓存框架,具有快速.精干等特点,也是Hibernate中默认的CacheProvider. 归纳一下它大概具有一下几个特点: 1. 快速. 2. 简单. 3. 多 ...

  6. PostgreSQL的权限查询

    查看哪些用户对表sns_log_member_b_aciton有哪些权限: sns_log=> \z sns_log_member_b_aciton Access privileges Sche ...

  7. Head First设计模式之策略模式(Strategy Pattern)

    前言: 刚刚开始学习设计模式,之前也接触过一些,但是从来都没有系统的学过,这次打算好好的学习一下.这里就当是对学习过程的一个记录.整理,以便可以在以后不时的温故知新. 这一节采用一个鸭子的示例,层层推 ...

  8. 使用jQuery+PHP+Mysql实现抽奖程序

    抽奖程序在实际生活中广泛运用,由于应用场景不同抽奖的方式也是多种多样的.本文将采用实例讲解如何利用jQuery+PHP+Mysql实现类似电视中常见的一个简单的抽奖程序. 查看演示 本例中的抽奖程序要 ...

  9. Sql Server Analysis Service 转换为UnknownMember的正确设置 (转载)

    转载: http://www.cnblogs.com/OpenCoder/p/4754447.html#commentform 在SSAS中事实表数据被归类到为UnknownMember 的时候分为两 ...

  10. 利用 chrome 做本地HTML5全屏应用

    现在HTML5已经很强大了,如何让网页看起来像本地应用呢?仅chrome浏览器就可以实现.(但当然只能使用HTML的功能,不能操作本地系统) 以百度为例: 使用chrome打开百度 https://w ...