※整個過程分成3個部份,
1.開通註冊憑證 cert、p12、pem等等有的沒的...
2.在程序中加入程序拿到devicetoken
3.在push server上發送訊息
我就不貼原理了...反正我也沒什麼看
1.開通註冊憑證 cert、p12、pem等等有的沒的...
這部份參考別人寫的~
http://blog.maxkit.com.tw/2014/03/iospush-notification-providerjava-apns.html
// Set Notification if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
----------上面這三句就是發出通知,會回應該機器的devicetoken,回應有2個function,一個是成功(會帶著devicetoken,另一個失敗,會帶回error,如下)
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken: (NSData *)newDeviceToken
{
//將Device Token由NSData轉換為字串,本來是長的像 <2223 5556 1111 .... > 8組還幾組的數字
NSString* deviceToken = [[[[[newDeviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""] retain];
NSLog(@"%@",deviceToken);
//取得device token!!
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError: (NSError *)err {
NSLog(@"deviceToken error----%@",err);
//有錯誤訊息會在這裡顯示
}
3.在push server上發送訊息
可以參考這個網址,裡面有一些已有的工具,跟選擇,我最後是用php自已push訊息
http://kyleiossdk.blogspot.tw/2013/07/ios-apple-push-notification-service.html
php的code~
<?php
// Production mode
//$certificateFile = 'apns-dis.pem';
//$pushServer = 'ssl://gateway.push.apple.com:2195';
//$feedbackServer = 'ssl://feedback.push.apple.com:2196';
// Sandbox mode
$certificateFile = 'apns_dev.pem';
$pushServer = 'ssl://gateway.sandbox.push.apple.com:2195';
$feedbackServer = 'ssl://feedback.sandbox.push.apple.com:2196';
// push notification
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert',dirname(__FILE__) . '\\' .$certificateFile);
//stream_context_set_option($streamContext, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
$pushServer,
$error,
$errorStr,
100,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$streamContext
);
// make payload
$payloadObject = array(
'aps' => array(
'alert' => 'Server Time:'.date('Y-m-d H:i:s'),
'sound' => 'default',
'badge' => 1
),
'custom_key' => 'custom_value'
);
$payload = json_encode($payloadObject);
$deviceToken = '3952935260f9fb47ca9535f103d4fb6ceddf0fdc5935b8ee56aac876b91a316f';
$expire = time() + 3600;
$id = time();
if ($expire) {
// Enhanced mode
$binary = pack('CNNnH*n', 1, $id, $expire, 32, $deviceToken, strlen($payload)).$payload;
} else {
// Simple mode
$binary = pack('CnH*n', 0, 32, $deviceToken, strlen($payload)).$payload;
}
$result = fwrite($fp, $binary);
if($expire)echo "has expire";
echo "</br>";
if($binary)echo "has binary";
echo "</br>";
print_r($result);
fclose($fp);
?>
這裡會需要一個apns_dev.pem,
1.將 aps_development.cer 轉換為 aps_development.pem
1
| openssl x509 -in aps_development.cer -inform DER -out aps_development.pem -outform PEM} |
1
| openssl pkcs12 -nocerts -out private_key.pem -in private_key.p12 |
3. 去除轉發密碼
1
| openssl rsa -out private_key_noenc.pem -in private_key.pem |
4. 合併 aps_development.pem 與 private_key_noenc.pem
1
| cat aps_development.pem private_key_noenc.pem > apns_dev.pem |
那你就得到apns_dev.pem了
(參考http://blog.yslin.tw/2011/07/apple-push-notification-provider-server.html)
這裡有幾個關鍵
這裡有幾個關鍵
//window下的寫法stream_context_set_option($streamContext, 'ssl', 'local_cert',dirname(__FILE__) . '\\' .$certificateFile);
//osx下的寫法
stream_context_set_option($streamContext, 'ssl', 'local_cert',$certificateFile);
也就是多加了個dirname(__FILE__) . '\\' .而已
php.ini 要打開
extension=php_openssl.dll
extension=php_openssl.dll
以及要打開ssl需要的模組~~~~~
但是做了这些,问题仍然不能解决,剩下的问题就是
Apache需要开启ssl模块,通过查看Apache的官方文档得知,使用ssl需要Apache开启三个支持模块分别是:- mod_include
- mod_cgi
- mod_expires
都做完之後呢,請"關掉"apache",再打開。
直接按重啟是沒有效果的!!
直接按重啟是沒有效果的!!
對了,當然也有可能是你pem有問題
你可以在osx底下這樣測試你的pem是否正常
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert aps_development.pem -key apns_dev.pem
祝大家基本用法,使用愉快
//最近重用一次,看一下自已寫的文章,發現有一些備註應該要加上去。以下。
※在輸出p12檔時,有時候會發現沒辦法選p12,key圈上下方各有2部份的選單,上面要先選登入 ,下面選憑證,這樣有privatekey的憑證前面會多一個箭頭,可以展開,那這一個憑證就能輸出p12了。
當你在輸出p12的時候 所需要輸入的驗證碼,之後再組成pem時會用到。
※使用
//最近重用一次,看一下自已寫的文章,發現有一些備註應該要加上去。以下。
※在輸出p12檔時,有時候會發現沒辦法選p12,key圈上下方各有2部份的選單,上面要先選登入 ,下面選憑證,這樣有privatekey的憑證前面會多一個箭頭,可以展開,那這一個憑證就能輸出p12了。
當你在輸出p12的時候 所需要輸入的驗證碼,之後再組成pem時會用到。
※使用
ssl需要Apache开启三个支持模块,在哪開啟呢?以我windows上的例子是在C:\AppServ\Apache2.2\conf\httpd.conf裡去找到並把前面的#去掉,我的模組資料夾在C:\AppServ\Apache2.2\modules。可 以先檢查你有沒有相關的模組。
沒有留言:
張貼留言