APNS推送通知的流程

参数原文:http://www.cocoachina.com/bbs/read.php?tid=20723 


我的APNS也是参考上面配置成功的。我会在原文的基础上,加上一点点的使用过程中的解析,以使它更加容易明白。


1、当我们重新装一个带有推送通知APP的时候,进入软件首先会弹出 XXXXXX推送之类,要你确定的,这如何实现呢?这很简单,我们在启动的 xxxxxDelegate.m文件中,类似的加进以下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	
	// 当用户第一次安装使用APP时,系统就会弹出确认推送通知        
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];  
        // other codes here.    
    return YES;
}


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

	// 这就是你得到的 deviceToken 字符串
	// 需要注意的是,deviceToken 是由APNS生成的,并不是由设备单独生成的。换句话说 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:

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格式:

openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12

openssl pkcs12 -nocerts -out key.pem -in key.p12

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

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

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

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

3.服务端push通知到ANPS. 在cocoachina找到了两种方法:


  1)php驱动。需要将ck.pem和php脚本放到server上。全部的php代码是:


<?php
$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; // icon 图标显示的条数
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 这是测试用的,也就是非正式证书用的。开发与正式使用的通知证书是完全不一样的两个证书,所以你还需要重复一次,以便生成正式的。
// 正式的把 sanbox去掉即可。
$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<br/>";
}
// 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);
?>

发送成功后,看到以下信息。 iPhone 将会弹出一个对话框。一个关闭按扭、一个查看按扭,显示message 的内容。

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

aps 你可以往里边塞东西。具体参数用法你可以看官方的文档,有很详细的说明。

$body['aps'] = array('alert' => $message, 'myid' =>$myid);

在使用过程中,必段要留意的地方

1、deviceToken 是由APNS生成的,并不是由设备本身生成,它可能会变。

2、苹果并不保证每一条信息推送通知,用户都能收到。所以不要用它来做很重要的东西,它仅仅是通知。。。

3、APNS 提供了一个接口,能够查询到传递通知失败的设备列表,具体参考官方文档。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值