apicloud是一款用html5封装的手机端的一个东西,消息推送就是我们经常说的手机打开一个软件后,该软件会出现相应的消息显示在通知栏,例如这样
怎么才能实现呢,其实完成下面几步就ok了
1、在极光推送网站( )注册帐号,并创建应用,获取APP_KEY
2、在应用的下写下
-
<?xml version="1.0" encoding="UTF-8"?>
-
-
<widget id="A6903547912689" version="0.0.1">
-
<name>helloworld</name>
-
<description>世界你好</description>
-
<author email="834827635@" href="">SK1995</author>
-
<content src=""/>
-
<preference name="appBackground" value="rgba(0,0,0,0)"/>
-
<preference name="windowBackground" value="rgba(0,0,0,0)"/>
-
<preference name="frameBackgroundColor" value="rgba(0,0,0,0)"/>
-
<preference name="autoLaunch" value="true"/>
-
<preference name="autoUpdate" value="true"/>
-
<preference name="smartUpdate" value="false"/>
-
<preference name="debug" value="true"/>
-
<preference name="statusBarAppearance" value="true"/>
-
<permission name="location"/>
-
<permission name="internet"/>
-
<access origin="*"/>
-
<feature name="ajpush">
-
<param name="app_key" value="15d39c8316d86519244de74f"/>
-
<param name="channel" value="17d3ea252e685d947a2911ac"/>
-
</feature>
-
</widget>
示例如下:
-
<feature name="ajpush">
-
<param name="app_key" value="123456789" />
-
<param name="channel" value="your channel" />
-
</feature>
-
1. app_key:通过极光推送网站获得
-
2. channel: 渠道号
渠道号随便写,没有关系
然后在页面加入启动就自动执行的方法,加上初始化和监听
-
function test() {
-
var ajpush = api.require('ajpush');
-
var param = {
-
alias : 'myalias',
-
tags : ['tag1', 'tag2']
-
};
-
ajpush.bindAliasAndTags(param, function(ret) {
-
var statusCode = ret.statusCode;
-
});
-
ajpush.init(function(ret) {
-
if (ret && ret.status) {
-
//success
-
ajpush.setListener(function(ret) {
-
var id = ret.id;
-
var title = ret.title;
-
var content = ret.content;
-
var extra = ret.extra;
-
console.log("hljs-string">",title=" + title + ",content=" + content + ",extra=" + extra);
-
});
-
}
-
});
-
}
Java中代码这么写,appkey依然是极光的appkey
-
package ;
-
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
-
public class Jiguang {
-
static JPushClient jPushClient=null;
-
static String masterSecret="17d3ea252e685d947a2911ac";
-
//static String appKey="15d39c8316d86519244de74f";
-
static String appKey="15d39c8316d86519244de74f";
-
static {
-
jPushClient =new JPushClient(masterSecret, appKey);
-
//PushPayload payload = buildPushObject_all_all_alert();
-
}
-
-
-
public static PushPayload buildPushObject_all_all_alert(){
-
return ().setPlatform(()).setAudience(("tag1")).setNotification(("乐吧一日游", "title", null)).build();
-
}
-
}
-
@RequestMapping("/push")
-
public static String push(HttpServletResponse response) throws APIConnectionException, APIRequestException{
-
PushPayload payload = Jiguang.buildPushObject_all_all_alert();
-
PushResult result = (payload);
-
("text/html");
-
("utf-8");
-
PrintWriter out=null;
-
try {
-
Map<String, String> maps=new HashMap<String, String>();
-
("name", "推送成功");
-
-
JSONArray array=(maps);
-
// JSONObject obj = (app);
-
out=();
-
(array);
-
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
();
-
}finally{
-
();
-
}
-
return null;
-
}
打开这个地址 http://192.168.199.120:8081/testMVC/就能进行推送了