IOS推送

1.证书
苹果开发者证书网页:
1.1 Certificates
      开发证书和发布证书下载,保存。development(自命名).cer和distribution(自命名).cer。
      双击打开钥匙串,导出证书,保存为development(自命名).cer和distribution(自命名).cer。
      若原本不支持推送,需要先开启push notification enabled,再生成或者更新发布证书。
1.2 Identifiers
      Push Notifications:Development和Distribution enable. 中间生成.certSigningRequest就不具体描述了。分别生成一个(development_xxx).cer和(production_xxx).cer保存下来。双击,打开钥匙串,在钥匙串中出现Apple Development IOS Push Service:xxxx和Apple Production IOS Push Service:xxxx两个证书,右击,导出"xxxxx"证书,保存。
      此时本地应有四个文件,两个.cer和两个.p12文件。
      文件用处:development_push(自命名).cer和development_push(自命名).p12供开发版推送使用。production_push(自命名).cer和production_push(自命名).p12供发布版推送使用。
1.3 Provisioning profiles
      装机测试用。
     Development:开发使用。

     Distribution:App Store:发布到app store使用。

 AdHoc-AdHoc使用。


2.工程代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 
[self.window makeKeyAndVisible];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    return YES;
 
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    UnitySendDeviceToken(deviceToken);
    NSString *str = [[[NSString stringWithFormat:@"%@", deviceToken]
                      stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
                     stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"token: %@", str);
}


3.推送测试


3.1 开发版推送测试:在应用服务器采用php的方式将消息推送给APNS
3.1.1制作.pem文件
     cd  进入证书所在目录


把.cer文件转换成.pem文件:


$ openssl x509 -in aps_developer_identity.cer -inform der


-out PushChatCert.pem


把私钥Push.p12文件转换成.pem文件:


$ openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12


Enter Import Password:


MAC verified OK


Enter PEM pass phrase:


Verifying – Enter PEM pass phrase:


你首先需要为.p12文件输入passphrase密码短语,这样OpenSSL可以读它。然后你需要键入一个新的密码短语来加密PEM文件。还是使用”pushchat”来作为PEM的密码短语。你需要选择一些更安全的密码短语。


注意:如果你没有键入一个PEM passphrase,OpenSSL将不会返回一个错误信息,但是产生的.pem文件里面将不会含有私钥。


最后。把私钥和证书整合到一个.pem文件里:


$ cat PushChatCert.pem PushChatKey.pem > ck.pem


为了测试证书是否工作,执行下面的命令:


$ telnet gateway.sandbox.push.apple.com 2195


Trying 17.172.232.226…


Connected to gateway.sandbox.push-apple.com.akadns.net.


Escape character is ‘^]’.


它将尝试发送一个规则的,不加密的连接到APNS服务。如果你看到上面的反馈,那说明你的MAC能够到达APNS。按下Ctrl+C 关闭连接。如果得到一个错误信息,那么你需要确保你的防火墙允许2195端口。


然后再次连接,这次用我们的SSL证书和私钥来设置一个安全的连接:


$ openssl s_client -connect gateway.sandbox.push.apple.com:2195


-cert PushChatCert.pem -key PushChatKey.pem


Enter pass phrase for PushChatKey.pem:


你会看到一个完整的输出,让你明白OpenSSL在后台做什么。如果连接是成功的,你可以键入一些字符。当你按下回车后,服务就会断开连接。如果在建立连接时有问题,OpenSSL将会给你一个错误消息,


ck.pem文件就是我们需要得到php连接APNS 的文件,将ck.pem和push.php放入同一目录上传到服务器,

push.php的代码如下:



 


<?php


// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';


// Put your private key's passphrase here:
$passphrase = 'abc123456';


// Put your alert message here:
$message = 'My first push test!';





$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);


// Open a connection to the APNS server
//这个为正是的发布地址
 //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
//这个是沙盒测试地址,发布到appstore后记得修改哦
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);


if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);


echo 'Connected to APNS' . PHP_EOL;


// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);


// Encode the payload as JSON
$payload = json_encode($body);


// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));


if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;


// Close the connection to the server
fclose($fp);
?>
注意点:
a.deviceToken:开发和发布获取的deviceToken是不一样的。
b.sandbox path:开发和发布的路径不一样。

c.不同的测试,记得更换不同的.pem文件。


3.1.2 运行php测试推送

php push.php



3.2 AdHoc推送测试:
Edit Scheme->ManageSchemes->Add,新增一个AdHoc名称(例:AdHoc)->OK
选中Build标签,选中AdHoc,添加AdHoc证书,若没有,回到苹果开发者网站,前往Provisioning Profile新增一个AdHoc profile。添加发布证书,使用真机运行app。
想办法获取设备的deviveToken(可以获取后保存至别处再查出),并使用php推送测试。
注:开发和发布用的deviceToken的不一致。


3.3发布推送
使用发布证书和对用的distribution profile上传app。


      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值