cordova新建android插件详细过程,有源码--Jpush服务器推送--2016-11-21

一. 准备工作

1. 环境搭建
 
2. 创建一个cordova项目
    cordova create jpushtest com.hpeJerry.jpushtest jpushtest
 
  
 
  
3. 为项目添加android平台
进入新建项目目录:       
    cd jpushtest
    cordova platform add android
工程根目录: 平台:
 插件:
4. 为当前项目安装插件: 
  》sqlite数据库
   cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage
  》极光推送官方支持的 PhoneGap/Cordova 插件
1. 极光推送账号注册应用
2. 添加   PhoneGap/Cordova 插件
cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=your_jpush_appkey
 
5. 为当前项目开发自定义插件jpushServerPlugin:
1. 安装plugman插件
    npm install –g plugman 
2. 创建插件
    plugman  create  --name  JpushServerPlugin  --plugin_ id  jpushServerPlugin --plugin_version 1.0.0
    执行命令会在当前目录生成插件文件夹
   

将一下jar包放入android目录下:
下载地址可参考:http://download.csdn.net/detail/yincheng886337/8537741#comment

然后接下来为plugin.xml添加如下代码:
        <source-file src="src/android/JpushServerPlugin.java" target-dir="src/com/jpushServer/plugin" />
	<source-file src="src/android/gson-2.2.4.jar" target-dir="libs" />
        <source-file src="src/android/slf4j-api-1.7.7.jar" target-dir="libs" />
        <source-file src="src/android/slf4j-log4j12-1.7.7.jar" target-dir="libs" />
        <source-file src="src/android/log4j-1.2.14.jar" target-dir="libs" />
        <source-file src="src/android/jpush-client-3.2.3" target-dir="libs" />
修改JpushServerPlugin.java为如下:
package com.jpushServer.plugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.Toast;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;


/**
 * This class echoes a string called from JavaScript.
 */
public class JpushServerPlugin extends CordovaPlugin {
    protected final Logger LOG = LoggerFactory.getLogger(JpushServerPlugin.class);
    private final String appKey ="8d22215b59f5d825779baaa8";
    private final String masterSecret = "823a530730f4a44d71bdf435";
    public JPushClient jpushClient = null;
    public void sendPush(String alert) {
        jpushClient = new JPushClient(masterSecret, appKey, 3);
         PushPayload payload = buildPushObject_all_all_alert(alert);
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error(
                    "Error response from JPush server. Should review and fix it. ",
                    e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
    }

    public PushPayload buildPushObject_all_all_alert(String alert) {
        return PushPayload.alertAll(alert);
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
        if(action.equals("sendJpushMsg")){
            String alert = args.getString(0);
                    //Toast.makeText(cordova.getActivity(), "show..."+alert, Toast.LENGTH_SHORT).show();
                    try
                    {
                        sendPush(alert); 
                        Toast.makeText(cordova.getActivity(), "正在推送...", Toast.LENGTH_SHORT).show();
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(cordova.getActivity(), "error..."+appKey, Toast.LENGTH_SHORT).show();
                    }
                    Toast.makeText(cordova.getActivity(), "推送成功", Toast.LENGTH_SHORT).show();
                    callbackContext.success("推送成功");
                    return true;
        }
        Toast.makeText(cordova.getActivity(), "推送失败", Toast.LENGTH_SHORT).show();
        callbackContext.error("推送失败");
        return false;
    }
}
 
   
将JpushServerPlugin.js修改如下:
var exec = require('cordova/exec');

exports.coolMethod = function(arg0, success, error) {
    exec(success, error, "JpushServerPlugin", "coolMethod", [arg0]);
};

var jpushServerFunc = function(){};
// arg1:成功回调
// arg2:失败回调
// arg3:将要调用类配置的标识
// arg4:调用的原生方法名
// arg5:参数,json格式

//特别注意:
arg3会在plugin.xml中config-file节点引用,不能乱写哦
/*
<config-file parent="/*" target="res/xml/config.xml">
	<feature name="
JpushMessage
">
		<param name="android-package" value="com.jpushServer.plugin.JpushServerPlugin" />
	</feature>
</config-file>
*/
jpushServerFunc.prototype.sendJpushMsg=function(alert,success, error) {
    exec(success, error, "JpushMessage", "sendJpushMsg", [alert]);
};

var showt = new jpushServerFunc();
module.exports = showt;
 
   
到此插件创建完毕。
 
   
3. 安装自定义插件
cordova plugin add C:\Cordova\JpushServerPlugin
 
   
5. 当前项目使用jpushServerPlugin:
/* 注意:插件中plugin.xml中设置 target="JpushServer",项目里就能通过window.JpushServer获取到插件对象
<js-module name="JpushServerPlugin" src="www/JpushServerPlugin.js"> <clobbers target="JpushServer" /> </js-module>
*/
var alert = '这是一条推送消息!';
window.
JpushServer.sendJpushMsg(
alert
,
function
(
msg
) {//target=JpushServer
alert
(
"success:"+msg
);
},
function
(
msg
) {
alert
(
"failed:"+msg
);
});
 



 



转载于:https://www.cnblogs.com/jiyuan12354/p/6085104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值