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代码如下
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2010, Nitobi Software Inc.
* Copyright (c) 2011, IBM Corporation
*/ package com.cordova.plugin.tencent; import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; import android.content.Context;
import android.content.Intent; import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.service.XGPushService; public class XinGe extends CordovaPlugin { @Override
// action 插件方法
// args 传递过来的参数,获取方法为args.getString(数组中的位置);
// callbackContext 回调函数
public boolean execute(String action, JSONArray args,
final CallbackContext callbackContext) throws JSONException {
Context context = this.cordova.getActivity();
// 判断要调用的方法
if (action.equals("registerPush")) {
// 获取第一个参数,用户名
String userName = args.getString(0);
// 调用方法
this.registerPush(context, callbackContext, userName);
return true;
} else if (action.equals("unregisterPush")) {
// 反注册,取消推送
XGPushManager.unregisterPush(context);
return true;
}
// 回调失败的函数
callbackContext.error("该方法不存在!");
return false;
} // 推送注册
private void registerPush(Context context,
final CallbackContext callbackContext, String userName) {
// 推送别名注册方法,可以根据别名进行推送
XGPushManager.registerPush(context, userName, new XGIOperateCallback() {
@Override
public void onSuccess(Object data, int flag) {
// 回调成功的函数
callbackContext.success();
} @Override
public void onFail(Object data, int errCode, String msg) {
// 回调失败的函数
callbackContext.error(msg);
}
});
// 在XGPushManager.registerPush(context)或其它版本的注册接口之后调用以下代码
// 使用ApplicationContext
// 兼容MIUI V6
Intent service = new Intent(context, XGPushService.class);
context.startService(service);
}
}
然后在res/xml/config.xml文件中加入以下配置
<feature name="<service_name>">
<param name="android-package" value="<full_name_including_namespace>" />
</feature>
在这里service_name值得是java代码中的类名,full_name_including_namespace只的是报名+类名,在这里我的配置是
<feature name="XinGe" >
<param
name="android-package"
value="com.cordova.plugin.tencent.XinGe" />
</feature>
2.js代码
在assets/www/cordova_plugins.js中进行修改
第4到13行,第160行是自行添加内容
cordova.define('cordova/plugin_list',
function (require, exports, module) {
module.exports = [
{
// js所在位置
"file": "plugins/com.cordova.plugin.tencent/www/android/xinGe.js",
// js中的id
"id": "com.cordova.plugin.tencent.xinGe",
// 通过window.tencentXinGe直接获取插件
"clobbers": [
"tencentXinGe"
]
},
//pg自带插件配置
{
"file": "plugins/org.apache.cordova.camera/www/CameraConstants.js",
"id": "org.apache.cordova.camera.Camera",
"clobbers": ["Camera"]
},
{
"file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
"id": "org.apache.cordova.camera.CameraPopoverOptions",
"clobbers": ["CameraPopoverOptions"]
},
{
"file": "plugins/org.apache.cordova.camera/www/Camera.js",
"id": "org.apache.cordova.camera.camera",
"clobbers": ["navigator.camera"]
},
{
"file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
"id": "org.apache.cordova.camera.CameraPopoverHandle",
"clobbers": ["CameraPopoverHandle"]
},
{
"file": "plugins/org.apache.cordova.file/www/DirectoryEntry.js",
"id": "org.apache.cordova.file.DirectoryEntry",
"clobbers": ["window.DirectoryEntry"]
},
{
"file": "plugins/org.apache.cordova.file/www/DirectoryReader.js",
"id": "org.apache.cordova.file.DirectoryReader",
"clobbers": ["window.DirectoryReader"]
},
{
"file": "plugins/org.apache.cordova.file/www/Entry.js",
"id": "org.apache.cordova.file.Entry",
"clobbers": ["window.Entry"]
},
{
"file": "plugins/org.apache.cordova.file/www/File.js",
"id": "org.apache.cordova.file.File",
"clobbers": ["window.File"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileEntry.js",
"id": "org.apache.cordova.file.FileEntry",
"clobbers": ["window.FileEntry"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileError.js",
"id": "org.apache.cordova.file.FileError",
"clobbers": ["window.FileError"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileReader.js",
"id": "org.apache.cordova.file.FileReader",
"clobbers": ["window.FileReader"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileSystem.js",
"id": "org.apache.cordova.file.FileSystem",
"clobbers": ["window.FileSystem"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileUploadOptions.js",
"id": "org.apache.cordova.file.FileUploadOptions",
"clobbers": ["window.FileUploadOptions"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileUploadResult.js",
"id": "org.apache.cordova.file.FileUploadResult",
"clobbers": ["window.FileUploadResult"]
},
{
"file": "plugins/org.apache.cordova.file/www/FileWriter.js",
"id": "org.apache.cordova.file.FileWriter",
"clobbers": ["window.FileWriter"]
},
{
"file": "plugins/org.apache.cordova.file/www/Flags.js",
"id": "org.apache.cordova.file.Flags",
"clobbers": ["window.Flags"]
},
{
"file": "plugins/org.apache.cordova.file/www/LocalFileSystem.js",
"id": "org.apache.cordova.file.LocalFileSystem",
"clobbers": ["window.LocalFileSystem"],
"merges": ["window"]
},
{
"file": "plugins/org.apache.cordova.file/www/Metadata.js",
"id": "org.apache.cordova.file.Metadata",
"clobbers": ["window.Metadata"]
},
{
"file": "plugins/org.apache.cordova.file/www/ProgressEvent.js",
"id": "org.apache.cordova.file.ProgressEvent",
"clobbers": ["window.ProgressEvent"]
},
{
"file": "plugins/org.apache.cordova.file/www/fileSystems.js",
"id": "org.apache.cordova.file.fileSystems"
},
{
"file": "plugins/org.apache.cordova.file/www/requestFileSystem.js",
"id": "org.apache.cordova.file.requestFileSystem",
"clobbers": ["window.requestFileSystem"]
},
{
"file": "plugins/org.apache.cordova.file/www/resolveLocalFileSystemURI.js",
"id": "org.apache.cordova.file.resolveLocalFileSystemURI",
"merges": ["window"]
},
{
"file": "plugins/org.apache.cordova.file/www/android/FileSystem.js",
"id": "org.apache.cordova.file.androidFileSystem",
"merges": ["FileSystem"]
},
{
"file": "plugins/org.apache.cordova.file/www/fileSystems-roots.js",
"id": "org.apache.cordova.file.fileSystems-roots",
"runs": true
},
{
"file": "plugins/org.apache.cordova.file/www/fileSystemPaths.js",
"id": "org.apache.cordova.file.fileSystemPaths",
"merges": ["cordova"],
"runs": true
},
{
"file": "plugins/org.apache.cordova.file-transfer/www/FileTransferError.js",
"id": "org.apache.cordova.file-transfer.FileTransferError",
"clobbers": ["window.FileTransferError"]
},
{
"file": "plugins/org.apache.cordova.file-transfer/www/FileTransfer.js",
"id": "org.apache.cordova.file-transfer.FileTransfer",
"clobbers": ["window.FileTransfer"]
},
{
"file": "plugins/org.apache.cordova.inappbrowser/www/inappbrowser.js",
"id": "org.apache.cordova.inappbrowser.inappbrowser",
"clobbers": ["window.open"]
}];
module.exports.metadata =
// TOP OF METADATA
{
//版本号
"com.cordova.tencentXinGe": "1.0.0",
"org.apache.cordova.camera": "0.3.3",
"org.apache.cordova.file": "1.3.1",
"org.apache.cordova.file-transfer": "0.4.7",
"org.apache.cordova.inappbrowser": "0.5.3"
}
// BOTTOM OF METADATA
});
在assets/www/plugins中新增com.cordova.plugin.tencent/www/android文件夹,其中新增xinGe.js,代码如下
//注册com.cordova.plugin.tencent.xinGe同cordova_plugins.js中id
cordova.define("com.cordova.plugin.tencent.xinGe", function (require, exports, module) {
var cordova = require('cordova'); var Tencent = function () {
//success 注册成功执行方法
//fail 注册失败执行方法
//推送注册
Tencent.prototype.registerPush = function (success, fail, userName) {
//'XinGe'对应我们在java文件中定义的类名
//registerPush对应我们在这个类中调用的自定义方法
//userName是我们客户端传递给这个方法的参数,是个string字段
cordova.exec(success, fail, 'XinGe', 'registerPush', [userName])
}
//推送反注册
Tencent.prototype.unregisterPush = function () {
cordova.exec(null, null, 'XinGe', 'unregisterPush', [])
}
}
var tencent = new Tencent(); module.exports = tencent; });
在应用中使用
//注意需要初始化pg才能起作用
//推送绑定
window.tencentXinGe.registerPush(function () {
console.log('注册成功!');
}, function (mes) {
console.log('注册失败!', mes);
},
//全局变量用户名
config.userMes.name);
这样我们就可以进行推送了
cordova(安卓)(腾讯信鸽注册绑定与反绑定) 插件开发的更多相关文章
- 吐槽在cocos2dx游戏接入腾讯信鸽的坑
腾讯信鸽是用来在后台推送消息给移动应用客户端使用,接入方法很简单,在信鸽官网注册个账号 http://xg.qq.com/xg,然后注册一个应用,在后台页面获得ACCESS ID, ACCESS KE ...
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
背水一战 Windows 10 之 绑定 x:Bind 绑定 x:Bind 绑定之 x:Phase 使用绑定过程中的一些技巧 示例1.演示 x:Bind 绑定的相关知识点Bind/BindDemo.x ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
- vb小菜一枚--------早期绑定和后期绑定
早期绑定和后期绑定 Visual Studio 2005 其他版本 将对象分配给对象变量时,Visual Basic 编译器会执行一个名为 binding 的进程.如果将对象分配给声明为特定对 ...
- KnockoutJS 3.X API 第四章 数据绑定(3) 控制流if绑定和ifnot绑定
if绑定目的 if绑定一般是格式是data-bind=if:attribute,if后所跟属性或表达式的值应为bool值(也可以是非bool值,当非空字符串时则为真),if绑定的作用与visible绑 ...
- KnockoutJS 3.X API 第四章 表单绑定(7) event绑定
目的 event绑定即为事件绑定,即当触发相关DOM事件的时候回调函数.例如keypress,mouseover或者mouseout等 例如: Mouse over me Details var vi ...
随机推荐
- selenium+python自动化80-文件下载(不弹询问框)
前言 上一篇是点弹出框上的按钮去保存文件,本篇介绍一种更加优雅的方法,加载Firefox和Chrome的配置文件,不弹出询问框后台下载. 一.FirefoxProfile 1.点下载的时候,如下图,如 ...
- 自动化运维之-PXE实现系统批量自动安装
转自:https://www.linuxidc.com/Linux/2017-10/147379.htm 本节索引 需求分析 PXE简介 整体方案 服务选择 功能实现 安装调试 错误分析 总结 1 需 ...
- how to use boost program options
From: http://www.radmangames.com/programming/how-to-use-boost-program_options If it so happens that ...
- VirtualBox中出现UUID have already exists 解决方法
虚拟机更换VDI文件,启动时会出现 "UUID already exists"的错误,这是因为删除虚拟机时候没有选择"删除所有",只是选择移除造成的. 方法一: ...
- 浅谈java构建工具的选择
在学校的时候还总是自己用eclipse自带的jar导出工具,然后人工来给项目打包,那是相当的原始. 而后工作了,项目中都是用ant,慢慢的开始学会使用这个工具.感觉就和脚本一样,很容易读懂,做项目构建 ...
- MUI class="mui-switch" 开关 默认为选中
<label >日期条件: </label> <div id="is_select_time" class="mui-switch mui- ...
- Rar安装包
@ECHO OFF If exist "%Temp%\~import.reg" ( Attrib -R -S -H "%Temp%\~import.reg" d ...
- 最近对latin-1这个字符集产生了不少好感
[简介] 最近我要解析一个数据库中间件的日志.这个中间件会在日志中记录SQL发往的后台DB ,执行耗时,对应的SQL:中间件直接把SQL写到 了日志中去,并没有对SQL进行适当的编码转换:理想情况下这 ...
- C# 给枚举类型增加一个备注特性
/// <summary> /// 备注特性 /// </summary> public class RemarkAttribute : Attribute { /// < ...
- lua -- 系统提示框
-- -- Author: chentong -- Date: 2014-3-24 -- 系统提示: -- 先做个简单的,在中上位置出现提示,逐渐消失,如果有新提示,则直接删除旧提示. -- 规则: ...