加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在SAE使用Apple Push Notification Service服务开发iOS应用

(2011-12-15 09:35:29)
标签:

it

作者:陈铮

 

1,在iOS开发者中心: iOS Provisioning Portal 创建一个AppID,如图:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

2,生成iOS Push Service证书,如图:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

按照上述提示操作:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用


回到网站,


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

选择刚才生成的文件,并上传:

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用





在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

点击“Download”下载iOS Push Service证书文件


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

3,导入证书文件到keychain双击即可


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

4,生成ck.pem

导出cert.p12
在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

导出key.p12:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

得到这样两个文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

接下来打开终端:

输入命令:openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12

 

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

然后输入命令:openssl pkcs12 -nocerts -out key.pem -in key.p12


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

 

最后合并成一个ck.pem文件:

输入命令:cat cert.pem key.pem > ck.pem


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

得到一个ck.pem文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

5, 生成并安装Profile文件:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

6, 上传ck.pemSAE 如图


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

7, 客户端程序:

设置profile:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

info.plist中设置Bundle identifier


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

将app注册notification里面, 并从APNS上获取测试机的deviceToken, 代码如下:

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

{

  ......  

   

    //注册到消息中心:   

    [[UIApplication sharedApplication]

     registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert |

                                         UIRemoteNotificationTypeNewsstandContentAvailability)];

       

    return YES;

}

#pragma mark -

#pragma mark APNS



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

 

    //获得 device token

    NSLog(@"deviceToken: %@", deviceToken);

   

    //获得 唯一标示

NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);

   

  

}



- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

 

    NSLog(@"Error in registration. Error: %@", error);

}





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

 

 

   

    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"] != nil) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知"

                                                        message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]

                                                       delegate:self

                                              cancelButtonTitle:@"确定"

                                              otherButtonTitles:nil];

        [alert show];

        [alert release];

    }

}



8, 服务器端程序:



<?php



include_once("saeapns.class.php");



//许可证id

$cert_id = 1;



//设备令牌

$device_token = "xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx";

 

$message = "测试消息 from SAE: " . date('Y-m-d H:i:s');



//消息体,格式详见iOS官方文档

$body = array(

'aps' => array('alert' => $message , 'badge' => 1, 'sound' => 'in.caf')

);



$apns = new SaeAPNS();

$result = $apns->push($cert_id , $body , $device_token);

 

if ($result != false) {



echo '发送成功';

} else {

 

echo '发送失败';

var_dump($apns->errno(), $apns->errmsg());

}

?>





9, 手机收到通知效果:


在SAE使用Apple <wbr>Push <wbr>Notification <wbr>Service服务开发iOS应用

实例代码下载地址:

http://vdisk.weibo.com/s/SbY2



 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有