|
前端js:
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("$UI/system/lib/cordova/cordova");
require("cordova!cn.jpush.phonegap.JPushPlugin");
var Model = function(){
this.callParent();
};
Model.prototype.modelLoad = function(event){
var we=this;
var td={rows:[]};
td.rows.push("tt");//设置tag名称
var alias="bb";//设置alias
var onDeviceReady = function(){
console.log("JPushPluginevice ready!")
window.plugins.jPushPlugin.setTagsWithAlias(td.rows,alias);
// initiateUI();
}
var onGetRegistradionID = function(data) {
try{
console.log("JPushPlugin:registrationID is "+data)
//$("#registrationid").html(data);
}
catch(exception){
console.log(exception);
}
}
// var onTagsWithAlias = function(event){
// try{
// console.log("onTagsWithAlias");
// var result="result code:"+event.resultCode+" ";
// result+="tags:"+event.tags+" ";
// result+="alias:"+event.alias+" ";
// we.comp("output1").set({value:result});
// }
// catch(exception){
// console.log(exception)
// }
// }
// var onOpenNotification = function(event){
// try{
// var alertContent
// if(device.platform == "Android"){
// alertContent=window.plugins.jPushPlugin.openNotification.alert;
// }else{
// alertContent= event.aps.alert;
// }
// //alert("open Notificaiton:"+alertContent);
// we.comp("output2").set({value:alertContent});
// }
// catch(exception){
// console.log("JPushPluginnOpenNotification"+exception);
// }
// }
// var onReceiveNotification = function(event){
// try{
//
// var eventContent="{";
// for(var key in event){
// if(key=="type"){
// break;
// }
// eventContent+=key+":"+JSON.stringify(event[key])+"\n"
// }
// eventContent+="}";
// we.comp("output2").set({value:eventContent});
//
// }
// catch(exeption){
// console.log(exception)
// }
// }
// var onReceiveMessage = function(event){
// try{
// var message = window.plugins.jPushPlugin.receiveMessage.message;
// //var extras = window.plugins.jPushPlugin.extras
//
// we.comp("output3").set({value:message});
//
// }
// catch(exception){
// console.log("JPushPluginnReceiveMessage-->"+exception);
// }
// }
var initiateUI = function(){
try{
window.plugins.jPushPlugin.init();
// window.plugins.jPushPlugin.resumePush();
window.plugins.jPushPlugin.getRegistrationID(onGetRegistradionID);
if(device.platform == "Android"){
window.plugins.jPushPlugin.setDebugModeFromIos();
window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
}else{
window.plugins.jPushPlugin.setDebugMode(true);
}
}
catch(exception){
console.log(exception);
}
}
//document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
document.addEventListener("deviceready", onDeviceReady, false);
// document.addEventListener("jpush.openNotification", onOpenNotification, false);
// document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
// document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
};
Model.prototype.button1Click = function(event){
// var tag1 = this.comp("input1").val();
// var tag2 = this.comp("input2").val();
// var td={rows:[]};
// if(tag1 != ""){
// td.rows.push(tag1);
// }
// if(tag2 != ""){
// td.rows.push(tag2);
// }
// var alias=this.comp("input3").val();
//
// window.plugins.jPushPlugin.setTagsWithAlias(td.rows,alias);
//
};
return Model;
});
后台代码将cn.jupsh.api下载下来拷贝下来复制到项目中,
package test;
import java.util.Collection;
import java.util.logging.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.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
public class PushExample {
// protected static final Logger LOG = (Logger) LoggerFactory.getLogger(PushExample.class);
private static final String appKey ="c2941a39f055d49cac7758a5";//在极光官网上获得的appKey
private static final String masterSecret = "b09751ca4e08bfc08aa5f475";//在极光官网上获得的masterSecret
public static final String TITLE = "一帖一图";//通知内容
public static final String ALERT = "hghgfhthrt";
public static final String MSG_CONTENT = "Test from API Example - msgContent";
public static final String REGISTRATION_ID = "1c9b974563af474b5dc18548";
public static final String TAG = "tag_api";
// static Collection<String>mm=new ;
public static void main(String[] args) {
testSendPush();
}
public static void testSendPush() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
// For push, all you need do is to build PushPayload object.
PushPayload payload =buildPushObject_android_tag_alertWithTitle();
/*
* Collection<String> strings = new LinkedList<String>();
* strings.add(REGISTRATION_ID);//配置多个客户端的极光注册id
* PushPayload payload = buildPushObject_all_registrationids_alert(strings);
*/
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println("Got result - " + result);
} catch (APIConnectionException e) {
System.out.println("Connection error. Should retry later. " + e);
} catch (APIRequestException e) {
System.out.println("Error response from JPush server. Should review and fix it. " + e);
System.out.println("HTTP Status: " + e.getStatus());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Msg ID: " + e.getMsgId());
}
}
//发给一个客户端
public static PushPayload buildPushObject_all_registrationid_alert() {
return PushPayload.newBuilder()
.setPlatform(Platform.all()) //设置平台-所有平台
.setAudience(Audience.registrationId(REGISTRATION_ID)) //设置受众-极光注册id
.setNotification(Notification.alert(ALERT)) //设置通知 - 消息
.build();
}
//多个客户端
public static PushPayload buildPushObject_all_registrationids_alert(Collection<String> strings) {
return PushPayload.newBuilder()
.setPlatform(Platform.all()) //设置平台-所有平台
.setAudience(Audience.registrationId(strings)) //设置受众-极光注册id-多个客户端
.setNotification(Notification.alert(ALERT)) //设置通知-推送信息
.build();
}
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag("tt"))//发送给前面设置的tag
.setNotification(Notification.android(ALERT, TITLE, null))
.build();
}
}
写的不好请见谅,至于推送过去的url我还不知道怎么设置,只能取默认到首页,希望对你有所帮助 |
|