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

标签:
it |
作者:陈铮
1,在iOS开发者中心: iOS Provisioning Portal 创建一个AppID,如图:
2,生成iOS Push Service证书,如图:
按照上述提示操作:
回到网站,
点击“Download”下载iOS Push Service证书文件
3,导入证书文件到keychain:双击即可
4,生成ck.pem
导出key.p12:
得到这样两个文件:
接下来打开终端:
输入命令:openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
然后输入命令:openssl pkcs12 -nocerts -out key.pem -in key.p12
最后合并成一个ck.pem文件:
输入命令:cat cert.pem key.pem > ck.pem
得到一个ck.pem文件:
5, 生成并安装Profile文件:
6, 上传ck.pem到SAE, 如图
7, 客户端程序:
设置profile:
在info.plist中设置Bundle identifier:
将app注册notification里面, 并从APNS上获取测试机的deviceToken, 代码如下:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOp
{
UIRemoteNotificationType
UIRemoteNotificationType
}
#pragma mark -
#pragma mark APNS
- (void)application:(UIApplication *)application
didRegisterForRemoteNoti
NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemo
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotifica
}
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, 手机收到通知效果:
实例代码下载地址: