Wex5+Java极光消息推送使用操作
Wex5+Java极光消息推送使用操作
一、首先注册登陆极光推送平台
二、创建应用,获取AppKey和Master Secret (必须)
1.点击创建应用
2.填写应用名称点击确定
3.集成Android SDK 消息推送服务 下一步
4.填写包名,此包名必须与wex5打包时填写的包名一致。提交并组装SDK
5.得到AppKey和Master Secret (必须)
(后面代码不可直接复制,需要根据自己的需求粘贴,因为我这是结合自己的项目做的,所以复制的时候需自己修改,否则会不好使的)
三、后台 javaSDK 服务设置
1.导入jar包,我使用的是idea的maven项目导入的在pom.xml中加入。
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.8</version>
</dependency>
2.新建java文件JpushClientUtil.Java改文件是主要消息推送文件。执行main方法即可完成(前提是前端需完成打包安装到手机上,后面会讲解前端操作)
此处为极光平台创建应用时生成的appKey与 masterSecret
private final static String appKey = “2b56226154facb04f572977b”;
private final static String masterSecret =“f6e1170fd0cb176bc2329e92”;
-------code-------
package com.jeeplus.modules.Util;
//package com.jeeplus.modules.Interface;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
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.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.*;
public class JpushClientUtil {
private final static String appKey = "2b56226154facb04f572977b";
private final static String masterSecret ="f6e1170fd0cb176bc2329e92";
//这个JpushClient是jar包中封装好的,如果你这里不能导入则证明你jar包没导入成功
private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
/**
* 推送给设备标识参数的用户
* @param registrationId 设备标识
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有安卓用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有IOS用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAll( String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
String message = e.getMessage();
System.out.println(message);
e.printStackTrace();
}
return result;
}
public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.setAlert(notification_title)
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("androidNotification extras key",extrasparam)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build()
)
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build()
)
.build();
}
private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_all_all_alert");
//创建一个IosAlert对象,可指定APNs的alert、title等字段
//IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
return PushPayload.newBuilder()
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
.setPlatform(Platform.all())
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
.setAudience(Audience.registrationId(registrationId))
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
//指定当前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("androidNotification extras key",extrasparam)
.build())
//指定当前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
//取消此注释,消息推送时ios将无法在锁屏情况接收
// .setContentAvailable(true)
.build())
.build())
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
.setTimeToLive(86400)
.build())
.build();
}
private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
return PushPayload.newBuilder()
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
.setPlatform(Platform.android())
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
.setAudience(Audience.all())
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
//指定当前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("androidNotification extras key",extrasparam)
.build())
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build())
.build();
}
private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");
return PushPayload.newBuilder()
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
.setPlatform(Platform.ios())
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
.setAudience(Audience.all())
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
//指定当前推送的android通知
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build())
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build())
.build();
}
public static void main(String[] args){
//发送给所有安卓用户
int stat1 = JpushClientUtil.sendToAllAndroid("testIos","testIos","this is a ios Dev test","");
//发送给所有用户
int stat2 = JpushClientUtil.sendToAll("testIos","testIos","this is a ios Dev test","");
//发送给所有IOS用户
int stat3 = JpushClientUtil.sendToAllIos("testIos","testIos","this is a ios Dev test","");
//推送给设备标识参数的用户
int stat4 = JpushClientUtil.sendToRegistrationId("REGISTRATION_ID","testIos","testIos","this is a ios Dev test","");
if(stat1==1){
System.out.println("发送给所有安卓用户success");
}
if(stat1==2){
System.out.println("发送给所有用户success");
}
if(stat1==3){
System.out.println("发送给所有IOS用户success");
}
if(stat1==4){
System.out.println("推送给设备标识参数的用户success");
}
}
}
3.新建Controller文件,(使用时需将JpushClientUtil.Java中的main方法注释掉)根据自己的项目创建在这里插入代码片
------code------
package com.jeeplus.modules.Interface;
import com.jeeplus.modules.Util.JpushClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "${adminPath}/jpush/")
public class push {
public static final String TITLE = "申通快递";
public static final String ALERT = "祝大家新春快乐";
public static final String MSG_CONTENT = "申通快递祝新老客户新春快乐";
//public static final String REGISTRATION_ID = "0900e8d85ef";
public static final String TAG = "tag_api";
@ResponseBody
@RequestMapping(value = "jpush")
public void testSendPush(String appKey ,String masterSecret,String REGISTRATION_ID) {
//发送给所有安卓用户
int stat1 = JpushClientUtil.sendToAllAndroid(TITLE,ALERT,MSG_CONTENT,"");
//发送给所有用户
int stat2 = JpushClientUtil.sendToAll(TITLE,ALERT,MSG_CONTENT,"");
//发送给所有IOS用户
int stat3 = JpushClientUtil.sendToAllIos(TITLE,ALERT,MSG_CONTENT,"");
//推送给设备标识参数的用户
int stat4 = JpushClientUtil.sendToRegistrationId(REGISTRATION_ID,TITLE,ALERT,MSG_CONTENT,"");
}
}
四、前端wex5操作
1.jpush.js文件在wex5自带案例外卖中。直接复制到自己的项目中
------code jpush.js------
define(function(require) {
var $ = require('jquery');
require("cordova!cordova-plugin-device");
var JPushInstance = function() {
if (window.plugins && JPush) {
document.addEventListener("deviceready", this.onDeviceReady.bind(this), false);
document.addEventListener("jpush.openNotification", this.onOpenNotification.bind(this), false);
document.addEventListener("jpush.receiveNotification", this.onReceiveNotification.bind(this), false);
document.addEventListener("jpush.receiveMessage", this.onReceiveMessage.bind(this), false);
}else{
alert("插件未安装或设备不支持,请在手机端重试!");
}
};
//hcr 解决第一次获取失败的问题
JPushInstance.prototype.getRegistrationID = function(){
var dtd = $.Deferred();
if (this.registrationID){
dtd.resolve(this.registrationID);
}else{
if (window.plugins && JPush){
JPush.getRegistrationID(function(registrationID) {
self.registrationID = registrationID;
if (self.registrationID){
dtd.resolve(self.registrationID);
}else{
dtd.reject();
}
});
}else{
dtd.reject();
}
}
return dtd.promise();
};
JPushInstance.prototype.onDeviceReady = function() {
var self = this;
JPush.init();
JPush.getRegistrationID(function(registrationID) {
self.registrationID = registrationID;
});
if (device.platform == "Android") {
JPush.setDebugMode(false);
JPush.setApplicationIconBadgeNumber(0);
} else {
JPush.setDebugMode(false);
JPush.setApplicationIconBadgeNumber(0);
}
};
JPushInstance.prototype.onOpenNotification = function(event) {
var alertContent;
if (device.platform == "Android") {
alertContent = JPush.openNotification.alert;
} else {
alertContent = event.aps.alert;
}
JPush.setApplicationIconBadgeNumber(0);
justep.Util.hint(alertContent);
};
JPushInstance.prototype.onReceiveNotification = function(event) {
var alertContent;
if(device.platform == "Android"){
alertContent = JPush.receiveNotification.alert;
}else{
alertContent = event.aps.alert;
}
JPush.setApplicationIconBadgeNumber(0);
justep.Util.hint(alertContent);
};
JPushInstance.prototype.onReceiveMessage = function() {
var message;
if(device.platform == "Android"){
message = JPush.receiveMessage.message;
}else{
message = event.content;
}
JPush.setApplicationIconBadgeNumber(0);
justep.Util.hint(message);
};
return new JPushInstance();
});
2.应用界面中导入文件与引入插件
var jpushInstance = require("./jpush"); //引入jpush.js文件
require(“cordova!jpush-phonegap-plugin”); //引入插件
require("$UI/system/lib/cordova/cordova");//cordova插件
3.获取设备标识registration_id,并将id通过ajax请求传到后台。Ajax请求写在另一个文件中ajaxRequest.js中的ajaxr.ajaxFunction(jsonObj, action);方法里。Ajax请求可根据自己的项目需求写。
------code push.js------
define(function(require){
var $ = require("jquery");
var jpushInstance = require("./jpush");
require("cordova!jpush-phonegap-plugin");
require("$UI/system/lib/cordova/cordova");
var ajaxr = require("$UI/testjumppage/ajaxRequest");
var Model = function(){
this.callParent();
};
Model.prototype.sendBtnClick = function(event){
var input = this.comp("input1").val();
if(input !== "" || input !== undefined){
this.sendOrderPushMessage();
}
};
Model.prototype.sendOrderPushMessage = function() {
//jpush实例初始化后,完成客户端连接,得到registrationID,用于判断向哪个具体设备发送,该参数不能为空。经过反复测试,否则极光推送失败。
if(window.JSESSIONID === undefined ||window.JSESSIONID === ""){
var loginurl = require.toUrl("$UI/test/login/login.w");
this.comp("windowDialog1").open({
src : loginurl,
params : {
operator : ""
}
});
}else{
jpushInstance.getRegistrationID().done(function(id) {
alert(id);
var action = "/a/jpush/jpush";
var jsonObj = {};
jsonObj.REGISTRATION_ID = id;
var data = {};
data = ajaxr.ajaxFunction(jsonObj, action);
});
}
};
Model.prototype.modelParamsReceive = function(event){
};
return Model;
});
------code ajaxRequest.js------
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
var a={};
a.ajaxFunction = function(jsonObj,action){
var rs = {};
$.ajax({
"url" : "http://192.168.0.105:8084"+action+";JSESSIONID="+window.JSESSIONID+"?__ajax=true&mobileLogin=true",
"type" : 'post',
"cache": false,
"async" : false,
"dataType" : 'json',
"ContentType" : 'application/json',
"data" : jsonObj,
"success" : function(result) {
rs=result;
},
"error" : function(result){
//alert("数据加载失败!");
//alert("服务端返回异常 ,meaage:"+JSON.stringify(result));
}
});
return rs;
};
return a;
});
五、打包App
(1) 在Native上右键-新建-创建本地App
发布模式选择第一个
填写应用名称
下一步
(2)选择服务地址和UI资源
Web服务这里选择的是我本地服务
Web路径
首页
选择资源,下一步
(3)配置应用信息
版本号:根据自己的需求填写
应用包名:com.jigunag(此包名要与极光平台上创建应用时填写的包名一致)
(4)配置开发者信息和证书
根据自己的需求填写。
(5)设置屏幕选项
(6)选择打包的本地插件
填写极光平台上创建应用获取的appkey
(7)配置更新信息下一步
(8)点击完成后启动app生成向导
(9)等待下一步
(10)下一步
(11)等待完成
(12)完成后扫描二维码安装或者复制app.apk文件到手机中安装。
完成后再Native中生成项目。
(13)这里使用的是将app.apk拖拽到模拟器中安装。这里使用的是“夜神模拟器”。
”
(14)发送成功.
(15)在极光平台上可以查看(这里我是用的模拟器和手机所以有两条数据)广播发送全部时可能会有条数限制。
(16)至此讲解完毕,如有什么不对的欢迎补充。