zoukankan      html  css  js  c++  java
  • cordova(安卓)(腾讯信鸽注册绑定与反绑定) 插件开发

    腾讯信鸽快速开发指南

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

    这样我们就可以进行推送了

  • 相关阅读:
    HtmlEncode 和 HtmlDecode
    Visual Studio .Net 的一些小技巧(2)
    Array和ArrayList的区别
    c#中 ?? 是什么意思?
    带有图片预览功能的上传表单 上传预览
    Js实现Repeater全选/反选 功能 终极解决方案
    处理URL重写后postback重写失效的问题 .browser文件
    在TreeView中使用CheckBox(c#)
    SQL操作全集
    智能仓库管理系统方案(一)
  • 原文地址:https://www.cnblogs.com/mlzs/p/4105457.html
Copyright © 2011-2022 走看看