AeroGear UnifiedPush Server,支援Android、iOS的Push Notification Server

AeroGear UnifiedPush Server是一套JBoss的開放套件子專案,可以支援Android、iOS的Push Notification。
直接在OpenShift(https://openshift.redhat.com)申請一個免費的帳號即可快速安裝AeroGear。
OpenShift是著名的Linux軟體商RedHad所提供的雲端服務,目前提供一個免費方案(一個免費帳號支援三種平台)可以使用,
支援快速安裝多種環境,例如:AeroGear、JBoss、PHP+MySQL、Drupal、WordPress、Perl、Ruby…等,
但是不支援FTP及後台管理介面,畢竟它並不是一個提供免費的網站空間,而是提供一個供程式開發人員免費的主機使用。


因為我只有Windows的環境,所以只有測試Android的GCM服務而已。
首先登入AeroGear,新增一個Application



第二步,進入新增的Application,新增Android的Variants。如果同時有iOS的推播需求,也可以在這裡再新增iOS的Variants


這裡選擇Android,並且在「Google Cloud Messaging Key」中輸入在Google Developer Console中憑證的伺服器應用程式的金鑰(Server Key),「Project Number」則輸入在Google Developer Console中的專案ID(Project ID),這樣AeroGear就完成了。


完成設定之後,在Android的Variants將加號點開,把下圖紅色框線的訊息它記錄下來


第三步則必須在Android的裝置上撰寫註冊GCM及AeroGear的程式碼,註冊GCM主要是取得Registration ID,並將Registration ID傳給AeroGear記錄,GCM的詳細流程可以搜尋Google。
因為我是用Phonegap來寫Android的程式,所以以Phonegap的步驟來說明程式

1、新增專案
phonegap create test05

2、加入AeroGear專為Phonegap(Cordova)開發的plugin
phonegap plugin add org.jboss.aerogear.cordova.push

3、查詢安裝的plugin,發現不只安裝一個
phoegap plugin list

4、將所有的plugin增加到Phonegap專案,將下列的字串加到config.xml
<gap:plugin name="org.jboss.aerogear.cordova.push 1.0.2" />
<gap:plugin name="com.vladstirbu.cordova.promise 1.0.0" />
<gap:plugin name="com.google.playservices 19.0.0" />
<gap:plugin name="android.support.v4 1.0.0" />    

5、程式範例:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=medium-dpi" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.4.min.css" />
        <title></title>
    </head>
    <body>
         <a href="#"  id="a_reg" class="ui-btn  ui-corner-all ">推播註冊</a>
          <br /><br />
          <a href="#"  id="a_unreg" class="ui-btn  ui-corner-all ">取消註冊</a>
          <br /><br />
        <a href="#" id="a_exit" class="ui-btn  ui-corner-all ">離開</a>
          <br /> <br />
          <p id="showText"></P>    

    </body>
</html>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.4.min.js"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>

<script type="text/javascript">
   
    //離開程式
    $("#a_exit").click(function(){
         if (navigator.app) {
            navigator.app.exitApp();
        } else if (navigator.device) {
            navigator.device.exitApp();
        }
    });
   
   
     //註冊GCM與AeroGear
     $("#a_reg").click(function() {
         try {
              //設定參數,這些參數都可以在Variants上找到,也就是剛剛記錄的部份
             var pushConfig = {
                    pushServerURL: "<Server URL>",
                    senderID: "<Project Number>",
                    variantID: "<variant ID>",
                    variantSecret: "<Secret>"
                };
            push.register(onNotification, successHandler, errorHandler, pushConfig);
         } catch(e) {
              alert(e);
          }
     });
   
   
     //取消註冊
     $("#a_unreg").click(function() {
         try {
             push.unregister(un_successHandler,un_errorHandler);
          } catch(e) {
              alert(e);
          }
     });
   
   
     //APP開啟期間如果接收到推播時的處理函數
     function onNotification(event) {
         alert(event.alert);
     }
   
     //註冊成功的處理函數
     function successHandler() {
         $("#showText").html("註冊成功");
     }
   
     function errorHandler(error) {
         $("#showText").html("註冊失敗:" + error);
     }
   
     //取消註冊成功的處理函數
     function un_successHandler() {
         $("#showText").html("取消註冊成功");
     }
   
     //取消註冊成功的處理函數
     function un_errorHandler(error) {
         $("#showText").html("註冊成功:" + error);
     }
   
</script>

第四步就可以為已經註冊過的Android推播訊息了。




Windows 11安裝時跳過網路連線