ios推送服务,php服务端
生活随笔
收集整理的這篇文章主要介紹了
ios推送服务,php服务端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?本文轉載至http://my.oschina.net/AStar/blog/176531
?
生成證書?證書生成參考:https://parse.com/tutorials/ios-push-notifications?
會生成三個文件?
xxx.cer?
xxx.certSigningRequest?
xxx.p12?
用PHP做推送需要?
推送證書生成。?
生成pem步驟:?
1.下載下來你配置好的推送證書aps_developer_identity.cer 文件。?
2.轉換 .cer 文件到 .pem 文件:?
openssl x509 -in aps_developer_identity.cer -inform der -out PushChatCert.pem?
3.在把你“鑰匙”推送證書導出成的.p12到.pem文件:?
openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12?
Enter Import Password:??
MAC verified OK?
Enter PEM pass phrase:??
Verifying - Enter PEM pass phrase:??
4.合并兩個pem文件:cat PushChatCert.pem PushChatKey.pem > ck.pem?
5.測試證書是否可用:?
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem?
****************客戶端推送處理,以下在AppDelegate文添加****************?
? //是否支持注冊推送?
?- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions?
{?
? ? /*............... */?
? ?//消息推送支持的類型?
? ? UIRemoteNotificationType types =(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert);?
? ? //注冊消息推送?
? ? [[UIApplication sharedApplication]registerForRemoteNotificationTypes:types];?
}?
發送推送的思路:先獲取手機的 deviceToken ,然后發送到我們的服務器?
?//獲取DeviceToken成功?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken?
{?
? ? NSLog(@"注冊device token");?
? ? NSLog(@"DeviceToken: {%@}",deviceToken);?
? ? //這里進行的操作,是將Device Token發送到服務端?
? ? NSString *tokenStr = [deviceToken description];?
? ? NSString *pushToken = [[[tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""]?
? ? ? ? ? ? ? ? ? ? ? ? ? ? stringByReplacingOccurrencesOfString:@">" withString:@""]?
? ? ? ? ? ? ? ? ? ? ? ? ? ?stringByReplacingOccurrencesOfString:@" " withString:@""];?
? ? //注冊到我們的服務器?
? ? NSString *url = @"http://api.xxxxxxcom/index.php?r=site/saveIphoneDeviceToken&device_token=";?
? ? url = [url stringByAppendingFormat:@"%@", pushToken];?
? ? [[AFOSCClient sharedClient]getPath:url parameters:Nil?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? success:^(AFHTTPRequestOperation *operation, id responseObject) {?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"注冊成功");?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} failure:^(AFHTTPRequestOperation *operation, NSError *error) {?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSLog(@"注冊失敗");?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}];?
}?
然后是處理接收推送消息?
?//注冊消息推送失敗?
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error?
{?
? ? NSLog(@"注冊消息推送失敗");?
? ? NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);?
}?
//處理收到的消息推送?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo?
{?
// ? ?NSLog(@"Receive remote notification : %@",userInfo);?
// ? ?NSLog(@"%@",[userInfo objectForKey:@"aps"]);?
? ? NSDictionary *aps = [userInfo objectForKey:@"aps"];?
// ? ?NSLog(@"%@",[aps objectForKey:@"alert"]);?
? ? NSString *msg = [aps objectForKey:@"alert"];?
// ? ?NSLog(@"%@",[userInfo objectForKey:@"alert"]);?
? ? UIAlertView *alert =?
? ? [[UIAlertView alloc] initWithTitle:@"溫馨提示"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?message:msg?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil?
? ? ? ? ? ? ? ? ? ? ?cancelButtonTitle:@"確定"?
? ? ? ? ? ? ? ? ? ? ?otherButtonTitles:nil];?
? ? [alert show];?
? ? [UIApplication sharedApplication].applicationIconBadgeNumber = 0;//把badge設置為0?
}?
****************服務器端push.php****************?
<?php?
//手機注冊應用返回唯一的deviceToken?
$deviceToken = 'eb87b209 6395a040 a1cf85e5 15b6e972 20ec883f 6b3ac80f 02b92a1a 58cf273e';?
//ck.pem通關密碼?
$pass = '13!@#23'; ???
//消息內容?
$message = '這是一條推送測試通知1'.time();?
//badge我也不知是什么?
//$badge = 1;?
//sound我也不知是什么(或許是推送消息到手機時的提示音)?
$sound = 'Duck.wav';?
//建設的通知有效載荷(即通知包含的一些信息)?
$body = array();?
$body['aps'] = array('alert' => $message);?
//if ($badge)?
// ?$body['aps']['badge'] = $badge;?
if ($sound)?
? $body['aps']['sound'] = $sound;?
//把數組數據轉換為json數據?
$payload = json_encode($body);?
echo strlen($payload),"\r\n";?
//下邊的寫法就是死寫法了,一般不需要修改,?
//唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195這個是沙盒測試地址,ssl://gateway.push.apple.com:2195正式發布地址?
$ctx = stream_context_create();?
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__) . '\\' . 'dev_ck.pem'); ??
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);?
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);?
if (!$fp) {?
? ? print "Failed to connect $err $errstr\n";?
? ? return;?
}?
else {?
? ?print "Connection OK\n<br/>";?
}?
// send message?
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;?
print "Sending message :" . $payload . "\n"; ??
fwrite($fp, $msg);?
fclose($fp);?
?>?
php服務端如果出問題,請檢查是否開通ssl模塊?
Apache需要開啟ssl模塊,通過查看Apache的官方文檔得知,使用ssl需要Apache開啟三個支持模塊分別是:?
mod_include?
mod_cgi?
mod_expires?
*注意:ios 的推送消息有256個字符長度限制;超出范圍不能發送
轉載于:https://www.cnblogs.com/Camier-myNiuer/p/4173679.html
總結
以上是生活随笔為你收集整理的ios推送服务,php服务端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Azure Bill
- 下一篇: MLlib 编程指导-spark-1.2