騰訊信鴿快速開發指南
http://developer.xg.qq.com/index.php/Android_SDK%E5%BF%AB%E9%80%9F%E6%8C%87%E5%8D%97
本文參考
http://bbs.phonegap100.com/thread-1160-1-1.html
1.java代碼
安卓項目目錄結構如下:
在這里我們開發一個騰訊推送注冊用戶插件,java代碼如下
1 /* 2 * PhoneGap is available under *either* the terms of the modified BSD license *or* the 3 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. 4 * 5 * Copyright (c) 2005-2010, Nitobi Software Inc. 6 * Copyright (c) 2011, IBM Corporation 7 */ 8 9 package com.cordova.plugin.tencent; 10 11 import org.json.JSONArray; 12 import org.json.JSONException; 13 import org.apache.cordova.CallbackContext; 14 import org.apache.cordova.CordovaPlugin; 15 16 import android.content.Context; 17 import android.content.Intent; 18 19 import com.tencent.android.tpush.XGIOperateCallback; 20 import com.tencent.android.tpush.XGPushManager; 21 import com.tencent.android.tpush.service.XGPushService; 22 23 public class XinGe extends CordovaPlugin { 24 25 @Override 26 // action 插件方法 27 // args 傳遞過來的參數,獲取方法為args.getString(數組中的位置); 28 // callbackContext 回調函數 29 public boolean execute(String action, JSONArray args, 30 final CallbackContext callbackContext) throws JSONException { 31 Context context = this.cordova.getActivity(); 32 // 判斷要調用的方法 33 if (action.equals("registerPush")) { 34 // 獲取第一個參數,用戶名 35 String userName = args.getString(0); 36 // 調用方法 37 this.registerPush(context, callbackContext, userName); 38 return true; 39 } else if (action.equals("unregisterPush")) { 40 // 反注冊,取消推送 41 XGPushManager.unregisterPush(context); 42 return true; 43 } 44 // 回調失敗的函數 45 callbackContext.error("該方法不存在!"); 46 return false; 47 } 48 49 // 推送注冊 50 private void registerPush(Context context, 51 final CallbackContext callbackContext, String userName) { 52 // 推送別名注冊方法,可以根據別名進行推送 53 XGPushManager.registerPush(context, userName, new XGIOperateCallback() { 54 @Override 55 public void onSuccess(Object data, int flag) { 56 // 回調成功的函數 57 callbackContext.success(); 58 } 59 60 @Override 61 public void onFail(Object data, int errCode, String msg) { 62 // 回調失敗的函數 63 callbackContext.error(msg); 64 } 65 }); 66 // 在XGPushManager.registerPush(context)或其它版本的注冊接口之后調用以下代碼 67 // 使用ApplicationContext 68 // 兼容MIUI V6 69 Intent service = new Intent(context, XGPushService.class); 70 context.startService(service); 71 } 72 }
然后在res/xml/config.xml文件中加入以下配置
1 <feature name="<service_name>"> 2 <param name="android-package" value="<full_name_including_namespace>" /> 3 </feature>
在這里service_name值得是java代碼中的類名,full_name_including_namespace只的是報名+類名,在這里我的配置是
1 <feature name="XinGe" > 2 <param 3 name="android-package" 4 value="com.cordova.plugin.tencent.XinGe" /> 5 </feature>
2.js代碼
在assets/www/cordova_plugins.js中進行修改
第4到13行,第160行是自行添加內容
1 cordova.define('cordova/plugin_list', 2 function (require, exports, module) { 3 module.exports = [ 4 { 5 // js所在位置 6 "file": "plugins/com.cordova.plugin.tencent/www/android/xinGe.js", 7 // js中的id 8 "id": "com.cordova.plugin.tencent.xinGe", 9 // 通過window.tencentXinGe直接獲取插件 10 "clobbers": [ 11 "tencentXinGe" 12 ] 13 }, 14 //pg自帶插件配置 15 { 16 "file": "plugins/org.apache.cordova.camera/www/CameraConstants.js", 17 "id": "org.apache.cordova.camera.Camera", 18 "clobbers": ["Camera"] 19 }, 20 { 21 "file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js", 22 "id": "org.apache.cordova.camera.CameraPopoverOptions", 23 "clobbers": ["CameraPopoverOptions"] 24 }, 25 { 26 "file": "plugins/org.apache.cordova.camera/www/Camera.js", 27 "id": "org.apache.cordova.camera.camera", 28 "clobbers": ["navigator.camera"] 29 }, 30 { 31 "file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js", 32 "id": "org.apache.cordova.camera.CameraPopoverHandle", 33 "clobbers": ["CameraPopoverHandle"] 34 }, 35 { 36 "file": "plugins/org.apache.cordova.file/www/DirectoryEntry.js", 37 "id": "org.apache.cordova.file.DirectoryEntry", 38 "clobbers": ["window.DirectoryEntry"] 39 }, 40 { 41 "file": "plugins/org.apache.cordova.file/www/DirectoryReader.js", 42 "id": "org.apache.cordova.file.DirectoryReader", 43 "clobbers": ["window.DirectoryReader"] 44 }, 45 { 46 "file": "plugins/org.apache.cordova.file/www/Entry.js", 47 "id": "org.apache.cordova.file.Entry", 48 "clobbers": ["window.Entry"] 49 }, 50 { 51 "file": "plugins/org.apache.cordova.file/www/File.js", 52 "id": "org.apache.cordova.file.File", 53 "clobbers": ["window.File"] 54 }, 55 { 56 "file": "plugins/org.apache.cordova.file/www/FileEntry.js", 57 "id": "org.apache.cordova.file.FileEntry", 58 "clobbers": ["window.FileEntry"] 59 }, 60 { 61 "file": "plugins/org.apache.cordova.file/www/FileError.js", 62 "id": "org.apache.cordova.file.FileError", 63 "clobbers": ["window.FileError"] 64 }, 65 { 66 "file": "plugins/org.apache.cordova.file/www/FileReader.js", 67 "id": "org.apache.cordova.file.FileReader", 68 "clobbers": ["window.FileReader"] 69 }, 70 { 71 "file": "plugins/org.apache.cordova.file/www/FileSystem.js", 72 "id": "org.apache.cordova.file.FileSystem", 73 "clobbers": ["window.FileSystem"] 74 }, 75 { 76 "file": "plugins/org.apache.cordova.file/www/FileUploadOptions.js", 77 "id": "org.apache.cordova.file.FileUploadOptions", 78 "clobbers": ["window.FileUploadOptions"] 79 }, 80 { 81 "file": "plugins/org.apache.cordova.file/www/FileUploadResult.js", 82 "id": "org.apache.cordova.file.FileUploadResult", 83 "clobbers": ["window.FileUploadResult"] 84 }, 85 { 86 "file": "plugins/org.apache.cordova.file/www/FileWriter.js", 87 "id": "org.apache.cordova.file.FileWriter", 88 "clobbers": ["window.FileWriter"] 89 }, 90 { 91 "file": "plugins/org.apache.cordova.file/www/Flags.js", 92 "id": "org.apache.cordova.file.Flags", 93 "clobbers": ["window.Flags"] 94 }, 95 { 96 "file": "plugins/org.apache.cordova.file/www/LocalFileSystem.js", 97 "id": "org.apache.cordova.file.LocalFileSystem", 98 "clobbers": ["window.LocalFileSystem"], 99 "merges": ["window"] 100 }, 101 { 102 "file": "plugins/org.apache.cordova.file/www/Metadata.js", 103 "id": "org.apache.cordova.file.Metadata", 104 "clobbers": ["window.Metadata"] 105 }, 106 { 107 "file": "plugins/org.apache.cordova.file/www/ProgressEvent.js", 108 "id": "org.apache.cordova.file.ProgressEvent", 109 "clobbers": ["window.ProgressEvent"] 110 }, 111 { 112 "file": "plugins/org.apache.cordova.file/www/fileSystems.js", 113 "id": "org.apache.cordova.file.fileSystems" 114 }, 115 { 116 "file": "plugins/org.apache.cordova.file/www/requestFileSystem.js", 117 "id": "org.apache.cordova.file.requestFileSystem", 118 "clobbers": ["window.requestFileSystem"] 119 }, 120 { 121 "file": "plugins/org.apache.cordova.file/www/resolveLocalFileSystemURI.js", 122 "id": "org.apache.cordova.file.resolveLocalFileSystemURI", 123 "merges": ["window"] 124 }, 125 { 126 "file": "plugins/org.apache.cordova.file/www/android/FileSystem.js", 127 "id": "org.apache.cordova.file.androidFileSystem", 128 "merges": ["FileSystem"] 129 }, 130 { 131 "file": "plugins/org.apache.cordova.file/www/fileSystems-roots.js", 132 "id": "org.apache.cordova.file.fileSystems-roots", 133 "runs": true 134 }, 135 { 136 "file": "plugins/org.apache.cordova.file/www/fileSystemPaths.js", 137 "id": "org.apache.cordova.file.fileSystemPaths", 138 "merges": ["cordova"], 139 "runs": true 140 }, 141 { 142 "file": "plugins/org.apache.cordova.file-transfer/www/FileTransferError.js", 143 "id": "org.apache.cordova.file-transfer.FileTransferError", 144 "clobbers": ["window.FileTransferError"] 145 }, 146 { 147 "file": "plugins/org.apache.cordova.file-transfer/www/FileTransfer.js", 148 "id": "org.apache.cordova.file-transfer.FileTransfer", 149 "clobbers": ["window.FileTransfer"] 150 }, 151 { 152 "file": "plugins/org.apache.cordova.inappbrowser/www/inappbrowser.js", 153 "id": "org.apache.cordova.inappbrowser.inappbrowser", 154 "clobbers": ["window.open"] 155 }]; 156 module.exports.metadata = 157 // TOP OF METADATA 158 { 159 //版本號 160 "com.cordova.tencentXinGe": "1.0.0", 161 "org.apache.cordova.camera": "0.3.3", 162 "org.apache.cordova.file": "1.3.1", 163 "org.apache.cordova.file-transfer": "0.4.7", 164 "org.apache.cordova.inappbrowser": "0.5.3" 165 } 166 // BOTTOM OF METADATA 167 });
在assets/www/plugins中新增com.cordova.plugin.tencent/www/android文件夾,其中新增xinGe.js,代碼如下
1 //注冊com.cordova.plugin.tencent.xinGe同cordova_plugins.js中id 2 cordova.define("com.cordova.plugin.tencent.xinGe", function (require, exports, module) { 3 var cordova = require('cordova'); 4 5 var Tencent = function () { 6 //success 注冊成功執行方法 7 //fail 注冊失敗執行方法 8 //推送注冊 9 Tencent.prototype.registerPush = function (success, fail, userName) { 10 //'XinGe'對應我們在java文件中定義的類名 11 //registerPush對應我們在這個類中調用的自定義方法 12 //userName是我們客戶端傳遞給這個方法的參數,是個string字段 13 cordova.exec(success, fail, 'XinGe', 'registerPush', [userName]) 14 } 15 //推送反注冊 16 Tencent.prototype.unregisterPush = function () { 17 cordova.exec(null, null, 'XinGe', 'unregisterPush', []) 18 } 19 } 20 var tencent = new Tencent(); 21 22 module.exports = tencent; 23 24 });
在應用中使用
1 //注意需要初始化pg才能起作用 2 //推送綁定 3 window.tencentXinGe.registerPush(function () { 4 console.log('注冊成功!'); 5 }, function (mes) { 6 console.log('注冊失敗!', mes); 7 }, 8 //全局變量用戶名 9 config.userMes.name);
這樣我們就可以進行推送了