phonegap(cordova) 入門 12----apns php 推送

這個是上接入門11 ,當不使用第三方推送的時候,ios 中要自己調用 蘋果的 APNs服務器,如果使用了信鴿,或者其他推送,那麼請略過


開始使用 .net (JdSoft.Apns.Notifications)  並提供了正確的    p12文件,後來好好的就不能發送了,好奇怪,折騰了兩天沒有找到答案

於是使用 php版  並提供正確的 pem文件

前提文件

1-----請求文件    也就是製作證書最早需要的 csr 文件

2-----下載推送證書     aps_production.cer  我這裏使用的是正式發佈版

3-----導出發佈證書或者開發證書下對應的私鑰  命名爲 Push.p12文件,並設置密碼  

pem 製作步驟如下

1、把.cer的SSL證書轉換爲.pem文件,執行命令:

openssl x509 -in aps_production.cer -inform der -out PushChatCert.pem 

2、把私鑰Push.p12文件轉化爲.pem文件:

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

3、對生成的這兩個pem文件再生成一個pem文件,來把證書和私鑰整合到一個文件裏:

cat PushChatCert.pem PushChatKey.pem > ck.pem 

最後貼上php provider 代碼

<?php  
    // 這裏是我們上面得到的deviceToken,直接複製過來(記得去掉空格)
    $deviceToken = 'd784d0f9b8b1fe1bd3b25437ee20202554572bfcea005bb8b355ef9a4c3127e1'; 
    // Put your private key's passphrase here:
    $passphrase = '123123'; 
    // Put your alert message here:
    $message = 'Hello!'; 
    $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);
?>

對應的app裏面獲取 deviceToken 的代碼

- (void)                                application:(UIApplication *)application
   didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"1: %@", deviceToken);
    NSString* result = [[[[deviceToken description]
       stringByReplacingOccurrencesOfString:@"<" withString:@""]
      stringByReplacingOccurrencesOfString:@">" withString:@""]
     stringByReplacingOccurrencesOfString:@" " withString:@""] ;
    
    
    NSLog(@"2: %@", result);
//    myDeviceToken =result;
     [MySingleton sharedSingleton].myDeveiceToken = result;//我這裏是混合開發,所以獲取之後傳遞給js 端調用,再保存到服務器端數據庫
    NSLog(@"3My token is: %@", [MySingleton sharedSingleton].myDeveiceToken);
   
    // re-post ( broadcast )
    NSString* token = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""]
                        stringByReplacingOccurrencesOfString: @">" withString: @""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];

    [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:token];
}


當然,如果你對apns 自主構建推送服務(不包括  使用第三方 比如百度雲,jpush,信鴿等)仍然有不清楚的地方,可以聯繫我,雖然不一定能解決,但可能會幫到你,節約時間是第一位,畢竟對於我們來說時間是腰椎的最大殺手,呵呵

qq  :[email protected]

email:[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章