先當作筆記本。。。
1.主要照著這一份做 https://developers.google.com/games/services/console/enabling
2.然後下載sample https://github.com/playgameservices/android-basic-samples
解開裡面android-basic-samples-master\Scripts\make_eclipse_compat.cmd
我先執行這cmd,它會說要在android-basic-samples-master這個的目錄下執行,也就是cd到這個目錄,再用\Scripts\make_eclipse_compat.cmd 這樣去執行
產生一個eclipse_compat目錄。
我先在eclipse裡 import 這目錄裡的 button_click項目。當然會有error
接著再import BaseGameUtils 這個lib,這個lib要去reference google play service的lib(這一步可以參考http://developer.android.com/google/play-services/setup.html)
接著在BaseGameUtils 進屬性,在android的tab裡,把is library勾起來(代表這個project是個library)
接著就可以在button_click(不過import進來是叫mainactivity)的項目裡reference 到 BaseGameUtils 這個lib了~good!
其他在控制台上的設定 有一些值得講的
產生sha1的key,這個一開始是用eclipse的debug.keystore,所以在C:\Users\使用者名稱\.android底下。keytool就是在java的bin底下有。
執行keytool -exportcert -alias androiddebugkey -keystore debug.keystore -list -v
哦對了,debugkey的密碼是android
接著復製sha1那一行就好了。
然後在控制台裡把所有的設定設定到能夠發佈為止。(我任務亂建的!)
在eclipse發生一個問題
[2014-10-24 03:43:06 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/android/gms/R$attr;
[2014-10-24 03:43:06 - MainActivity] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/android/gms/R$attr;
[2014-10-24 03:43:31 - BaseGameUtils] R.java was removed! Recreating R.java!
說是gms下的r.java重覆定義了,我砍了button_click底下的不行,只好砍BaseGameUtils 底下的gms/r.java,嘛 也是過了。
後來就run起來了,不過還沒試多人連線的部份,明天拿另一台機器再來試。
感覺不錯 沒花幾個鐘。
2014年10月23日 星期四
2014年10月10日 星期五
ios推播教學&php push server
※程式碼的部份很少,相較於android實在是少的美妙極了
※整個過程分成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
2.在程序中加入程序拿到devicetoken
----------上面這三句就是發出通知,會回應該機器的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
2. 將 private_key.p12 轉換為 private_key.pem
3. 去除轉發密碼
4. 合併 aps_development.pem 與 private_key_noenc.pem
※整個過程分成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。可 以先檢查你有沒有相關的模組。
訂閱:
文章 (Atom)