IOS 推送消息 php做推送服務端

IOS推送消息是許多IOS應用都具備的功能,最近也在研究這個功能,參考了很多資料終於搞定了,下面就把步驟拿出來分享下:

 

 

 

iOS消息推送的工作機制可以簡單的用下圖來概括:


 

Provider是指某個iPhone軟件的Push服務器,APNS是Apple Push Notification Service的縮寫,是蘋果的服務器。

 

上圖可以分爲三個階段:

第一階段:應用程序把要發送的消息、目的iPhone的標識打包,發給APNS。 

第二階段:APNS在自身的已註冊Push服務的iPhone列表中,查找有相應標識的iPhone,並把消息發送到iPhone。 

第三階段:iPhone把發來的消息傳遞給相應的應用程序,並且按照設定彈出Push通知。

 

從上圖我們可以看到:

1、應用程序註冊消息推送。

2、iOS從APNS Server獲取device token,應用程序接收device token。

3、應用程序將device token發送給PUSH服務端程序。

4、服務端程序向APNS服務發送消息。

5、APNS服務將消息發送給iPhone應用程序。

 

無論是iPhone客戶端和APNS,還是Provider和APNS,都需要通過證書進行連接。

 

下面我介紹一下幾種用到的證書。

 

一、CSR文件

 

1、生成Certificate Signing Request(CSR)


 

2、填寫你的郵箱和常用名稱,並選擇保存到硬盤。


 

點擊繼續:


 

這樣就在本地生成了一個Push.certSigningRequest文件。

 

二、p12文件

 

1、導出密鑰。



 

2、輸入你的密碼。

 

 

這樣就生成了一個Push.p12文件。

 

三、SSL certificate文件

 

1、用你付過費的帳號登錄到iOS Provisioning Portal,並新建一個App ID,這個過程可以參考:iOS應用的真機調試,這樣就會生成下面這條記錄:


 

2、點擊右側的Configure:


 

3、點擊Development Push SSL Certificate一行後的Configure:

 

 

4、點擊Continue:


 

5、選擇前面生成好的Push.certSigningRequest文件,點擊Generate,出現如下所示的頁面:


 

6、點擊Continue:


 

 

7、點擊Download,並將文件命名爲aps_developer_identity.cer。

 

8、點擊Done,你會發現狀態變成了Enabled:


 

 

到現在爲止,我們已經生成了三個文件:

1、Push.certSigningRequest

2、Push.p12

3、aps_developer_identity.cer

 

雙擊aps_developer_dientity.cer 註冊到你的鑰匙串中,這樣你的鑰匙串中就會有


二、準備profile證書,因爲推送消息只能再真機上測試,所以要建一個profile證書



點擊"new profile"爲上面新建的APP ID建個profile ,成功之後下載*_Dev_Profile.mobileprovision

雙擊將其加入到xcode 的Provisioning Profiles 中,這裏有一點要注意,再將這個加入xcode之前如果之前已經加入過一定要把之前加入的刪掉,如果有多個的話會出錯。

 

三、工程代碼

到這裏證書已經準備完畢,接下來,我們在xcode中新建一個測試工程,注意設置工程的Bundle Identifier必須與上面建的APP ID 裏的相同


在didFinishLaunchingWithOptions 中加入一下代碼

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

 

[self.window makeKeyAndVisible];

    [[UIApplication sharedApplicationregisterForRemoteNotificationTypesUIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];

    return YES;

 

}

 

 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {


    NSLog(@"regisger success:%@", pToken);

    

    //註冊成功,deviceToken保存到應用服務器數據庫中

    

}


 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    // 處理推送消息

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"通知" message:@"我的信息" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:nilnil];

    [alert show];

    [alert release];

NSLog(@"%@", userInfo);

}


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"Regist fail%@",error); 

    

  

}

 

到這裏一切順利的話我們就可以在真機運行了,註冊成功我們會得到iphone 的deviceToken,

 

My token is:

<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>

 

四、在應用服務器採用php的方式將消息推送給APNS,

1、php連接APNS也是需要證書的,還記得我們上面獲得的幾個證書嗎?打開終端,對上面的證書做如下處理,

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);

?>


接下來我們訪問http://localhost/push/push.php


iphone就會接收到一條推送消息了,如果有問題的話就檢查上面的操作步驟,特別是加紅的部分

 

另外去除標記的方法爲,在viewDidApper中加入

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

    if(badge > 0)

    {

        badge--;

        [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    }

 

 

 

給一個參考地址:http://article.ityran.com/archives/194

 

分享到:  
評論
9 樓 flex_莫衝 2013-12-16  
不需要用到apnsphp 類庫。。挺好的。謝謝。
8 樓 joesenzhao 2013-08-12  
非常感謝,幫勒我大忙勒,一步一步終於成功勒
7 樓 20072432 2013-06-15  
您好,我的是可以了,但是還是有問題啊。
Warning: pack(): Type H: illegal hex digit s in apple.php on line 44
PHP Warning:  pack(): Type H: illegal hex digit z in apple.php on line 44


就是這句出問題
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


爲什麼呢?
6 樓 airzhangfish 2013-04-03  
非常感謝,按照您的操作方式一步一步來,終於成功了
5 樓 hhb19900618 2012-12-09  
你好 我想問一下 如果不把p12文件轉換成pem 把p12文件作爲證書會怎麼樣呢?
4 樓 zxs19861202 2012-10-25  
hhb19900618 寫道
兄弟 你好 爲啥我輸入最後的那個命令老是 報錯呢?
Error opening client certificate private key file PushChatKey.pem
712:error:02001002:system library:fopen:No such file or directory:/SourceCache/OpenSSL098/OpenSSL098-44/src/crypto/bio/bss_file.c:356:fopen('PushChatKey.pem','r')
712:error:20074002:BIO routines:FILE_CTRL:system lib:/SourceCache/OpenSSL098/OpenSSL098-44/src/crypto/bio/bss_file.c:358:
unable to load client certificate private key file

你運行過pushmebaby的demo沒?

你執行哪個命令報錯啊,我寫的東西全部測試過,都ok
3 樓 hhb19900618 2012-10-24  
兄弟 你好 爲啥我輸入最後的那個命令老是 報錯呢?
Error opening client certificate private key file PushChatKey.pem
712:error:02001002:system library:fopen:No such file or directory:/SourceCache/OpenSSL098/OpenSSL098-44/src/crypto/bio/bss_file.c:356:fopen('PushChatKey.pem','r')
712:error:20074002:BIO routines:FILE_CTRL:system lib:/SourceCache/OpenSSL098/OpenSSL098-44/src/crypto/bio/bss_file.c:358:
unable to load client certificate private key file

你運行過pushmebaby的demo沒?
2 樓 zxs19861202 2012-10-12  
czc9180 寫道
你好

我按您的步聚,執行程序成功後

顯示 Connected to APNS Message successfully delivered 

但是ipad 沒有接收到消息,請問這是什麼原因?



好好的檢查一下你的各個步驟,設備的id獲取到了沒?
1 樓 czc9180 2012-10-11  
你好 

我按您的步聚,執行程序成功後 

顯示 Connected to APNS Message successfully delivered 

但是ipad 沒有接收到消息,請問這是什麼原因?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章