index.html 8.69 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
<!DOCTYPE html>
<html>
  
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>JPush Phonegap Simple Demo</title>
      <link href="css/jquery.mobile-1.1.1.css" rel="stylesheet" type="text/css" />
      <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript" src="js/jquery.mobile-1.1.1.js"></script>
      <script type="text/javascript" src="cordova.js"></script>
      <script type="text/javascript">

        var onDeviceReady = function() {
          document.addEventListener("jpush.receiveRegistrationId", function (event) {
                alert("receiveRegistrationId" + JSON.stringify(event));
                $("#registrationId").html(event.registrationId);
            }, false)

          initiateUI();
        };
      
        var getRegistrationID = function() {
          window.JPush.getRegistrationID(onGetRegistrationID);
        };
      
        var onGetRegistrationID = function(data) {
          try {
            console.log("JPushPlugin:registrationID is " + data);
            
            if (data.length == 0) {
              var t1 = window.setTimeout(getRegistrationID, 1000);
            }

            $("#registrationId").html(data);
          } catch (exception) {
            console.log(exception);
          }
        };
      
        var onTagsWithAlias = function(event) {
          try {
            console.log("onTagsWithAlias");
            var result = "result code:" + event.resultCode + " ";
            result += "tags:" + event.tags + " ";
            result += "alias:" + event.alias + " ";
            $("#tagAliasResult").html(result);
          } catch (exception) {
            console.log(exception)
          }
        };
      
        var onOpenNotification = function(event) {
          try {
            var alertContent;
            if (device.platform == "Android") {
              alertContent = event.alert;
            } else {
              alertContent = event.aps.alert;
            }
            alert("open Notification:" + alertContent);
          } catch (exception) {
            console.log("JPushPlugin:onOpenNotification" + exception);
          }
        };
        
        var onReceiveNotification = function(event) {
          try {
            var alertContent;
            if (device.platform == "Android") {
              alertContent = event.alert;
            } else {
              alertContent = event.aps.alert;
            }
            $("#notificationResult").html(alertContent);
          } catch (exception) {
            console.log(exception)
          }
        };
      
        var onReceiveMessage = function(event) {
          try {
            var message;
            if (device.platform == "Android") {
              message = event.message;
            } else {
              message = event.content;
            }
            $("#messageResult").html(message);
          } catch (exception) {
            console.log("JPushPlugin:onReceiveMessage-->" + exception);
          }
        };
      
        var initiateUI = function() {
          try {
            window.JPush.init();
            window.JPush.setDebugMode(true);
            window.setTimeout(getRegistrationID, 1000);

            if (device.platform != "Android") {
              window.JPush.setApplicationIconBadgeNumber(0);
            }
          } catch (exception) {
            console.log(exception);
          }

          $("#setTags").click(function(ev) {
            try {
              var tag1 = $("#tagText1").val()
              var tag2 = $("#tagText2").val()
              var tag3 = $("#tagText3").val()
              var tags = []
                                            
              if (tag1) {
                tags.push(tag1)
              }
              if (tag2) {
                tags.push(tag2)
              }
              if (tag3) {
                tags.push(tag3)
              }

              window.JPush.setTags({ sequence: 1, tags: tags },
                function (result) {
                  $("#tagsResult").html(result.tags)
                }, function (error) {
                  alert(error.code)
                })
            } catch (exception) {
              console.log(exception)
            }
          })

          $("#getAllTags").click(function (event) {
            window.JPush.getAllTags({ sequence: 2 },
              function (result) {
                $("#tagsResult").html(result.tags)
              }, function (error) {
                alert(error.code)
              })
          })

          $("#cleanTags").click(function (event) {
            window.JPush.cleanTags({ sequence: 2 },
              function (result) {
                alert(result.sequence)
                $("#tagsResult").html("")
              }, function (error) {
                alert(error.code)
              })
          })

          $("#setAlias").click(function (event) {
            var alias = $("#aliasText").val()
            window.JPush.setAlias({ sequence: 1, alias: alias },
              function (result) {
                $("#aliasResult").html(result.alias)
              }, function (error){
                alert(error.code)
              })
          })

          $("#getAlias").click(function (event) {
            window.JPush.getAlias({ sequence: 2 },
              function (result) {
                alert(JSON.stringify(result));
              }, function (error) {
                alert(error.code)
              })
          });

          $("#deleteAlias").click(function (event) {
            window.JPush.deleteAlias({ sequence: 3 },
              function (result) {
                alert(JSON.stringify(result));
              }, function (error) {
                alert(error.code)
              })
          });
        };
      
        document.addEventListener("deviceready", onDeviceReady, false);
        document.addEventListener("jpush.openNotification", onOpenNotification, false);
        document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
        document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
      </script>
  </head>
  
  <body>
    <div data-role="page" id="page">
      <div data-role="content">
        <form>
          <div class="ui-body ui-body-b">
            <div data-role="fieldcontain">
              <center>
                <h3>JPushPlugin Example</h3>
              </center>
              <span name="alias" id="alias"></span>
              <hr/>
              <label>RegistrationID: </label>
              <label id="registrationId">null</label>
            </div>
            <div data-role="fieldcontain">
              <label>Tags: </label>
              <table>
                <tr>
                  <td>
                    <input type="text" id="tagText1" />
                  </td>
                </tr>
                <tr>
                  <td>
                    <input type="text" id="tagText2" />
                  </td>
                </tr>
                <tr>
                  <td>
                    <input type="text" id="tagText3" />
                  </td>
                </tr>
              </table>
              <label>Alias: </label>
              <table>
                <tr>
                  <td>
                    <input type="text" id="aliasText" />
                  </td>
                </tr>
              </table>
            </div>

            <div data-role="fieldcontain">
              <input type="button" id="setTags" value="Set tags" />
              <input type="button" id="getAllTags" value="Get all tags" />
              <input type="button" id="cleanTags" value="Clean tags" />
            </div>
              
            <div data-role="fieldcontain">
              <input type="button" id="setAlias" value="Set alias" />
              <input type="button" id="getAlias" value="Get alias" />
              <input type="button" id="deleteAlias" value="Delete alias" />
            </div>

            <div data-role="fieldcontain">
              <label id="tagsPrompt">设置 Tag 的结果:</label>
              <label id="tagsResult">null</label>
            </div>
            <div data-role="fieldcontain">
              <label id="aliasPrompt">设置 Alias 的结果:</label>
              <label id="aliasResult">null</label>
            </div>
            <div data-role="fieldcontain">
              <label id="notificationPrompt">接受的通知内容:</label>
              <label id="notificationResult">null</label>
            </div>
            <div data-role="fieldcontain">
              <label id="messagePrompt">接受的自定义消息:</label>
              <label id="messageResult">null</label>
            </div>
          </div>
        </form>
      </div>
    </div>
  </body>
</html>