本文主要是介绍apicloud极光推送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//在极光注册上传应用的 appKey 和 masterSecret private final static String appKey = "*****";private final static String masterSecret = "****";private final static int maxtrytimes = 3;private static JPushClient jpushClient = new JPushClient(masterSecret, appKey, maxtrytimes);public PushPayload buildPushObject(String title, String content,String from, Audience audience ,Map extra,Boolean sendNotify) {//推送对象构造AndroidNotification android = AndroidNotification.newBuilder().setTitle(title).setAlert(content).addExtra("from", from).addExtra("sendtime", System.currentTimeMillis()).build();IosNotification ios = IosNotification.newBuilder().incrBadge(1).addExtra("from", from).setSound("default").addExtra("sendtime", System.currentTimeMillis()).build();Notification notification = Notification.newBuilder().setAlert(content).addPlatformNotification(android).addPlatformNotification(ios).build();//通知构造Message msg;//消息构造if(extra==null){msg = Message.newBuilder().setTitle(title).setMsgContent(content).build();}else{msg = Message.newBuilder().setTitle(title).setMsgContent(content).addExtras(extra).build();//发送额外信息可用addExtras传map,也可直接用addExtra传键值对}if(sendNotify){return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(audience).setNotification(notification).setMessage(msg).build();}else{return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(audience).setMessage(msg).build();}}public static void send() throws APIConnectionException, APIRequestException{//testSend();MessagePush s = new MessagePush();Map extra = new HashMap();extra.put("title", "好消息【点击查看】");PushPayload payload = s.buildPushObject("好消息【点击查看】", "您有一条待办需要处理!","测试", Audience.all(),extra,false);PushResult result = s.jpushClient.sendPush(payload);System.err.println(result);}
//apicloud 测试极光推送成功
var jpush = null;apiready = function() {
initPush();
}function initPush(){// 实例化极光推送对象jpush = api.require('ajpush');// 初始化推送服务,只Android有效,ios上会自动初始化jpush.init(function(ret, err) {});//设置推送时间jpush.setPushTime({days: 1,startHour: 0,endHour: 23}, function(ret, err){});// 监听应用进入后台,通知jpush暂停事件api.addEventListener({name : 'pause'}, function(ret, err) {//统计-app暂停//jpush.onResume();});// 监听应用恢复到前台,通知jpush恢复事件api.addEventListener({name : 'resume'}, function(ret, err) {//统计-app恢复jpush.onResume();});// 监听通知被点击(Android)api.addEventListener({name : 'appintent'}, function(ret, err) {if (ret && ret.appParam.ajpush) {// 通知被点击后处理executeNotice(ret, "appintent");}});// 监听通知被点击(iOS应用处于后台)api.addEventListener({name : 'noticeclicked'}, function(ret, err) {if (ret && ret.value) {// 通知被点击后处理executeNotice(ret, "noticeclicked");}});// 消息监听jpush.setListener(function(re, er) {//alert($api.jsonToStr(re));if (re) {var tit = re.title; // 标题var con = re.content; // 内容var ex = {}; // 附加字段// 附加字段不为空if(re.extra){var ext = re.extra;ex = {"extra" : ext};}// 转为状态栏通知notification(tit, con, ex);}});
}// 监听到的消息转为状态栏通知
function notification(t, c, e){api.notification({vibrate : [300, 500], //震动时间节奏sound : 'default', //系统默认提示音light : false, //是否亮灯,需设备支持notify : {//状态栏通知title : t || "系统消息", //标题,默认值为应用名称,只Android有效content : c, //内容,默认值为'有新消息'extra : e, //传递给通知的数据,在通知被点击后,该数据将通过监听函数回调给网页updateCurrent : false//是否覆盖更新已有的通知,取值范围true|false。只Android有效}}, function(ret, err) {//if(ret){//api.alert(ret.id);//id为通知ID,可用于取消通知//}});
}// 通知被点击后处理
function executeNotice (ret, type) {var ajpush_obj; // 信息对象var extra = {}; // 附加字段if(type == "appintent"){ajpush_obj = ret.appParam.ajpush;if(!isEmptyObject(ajpush_obj)){if(!isEmptyObject(ajpush_obj.extra)){extra = ajpush_obj.extra;}}}else if(type == "noticeclicked"){ajpush_obj = ret.value;if(!isEmptyObject(ajpush_obj)){c = ajpush_obj.content;// 转为json字符串ajpush_obj = JSON.stringify(ajpush_obj);// 再转为json对象ajpush_obj = $.parseJSON(ajpush_obj);// 判断附加参数是否为空if(!isEmptyObject(ajpush_obj.extra)){//extra = $.parseJSON(ajpush_obj.extra);extra = ajpush_obj.extra;}}}// 附加字段不为空if(!isEmptyObject(extra)){if (api.systemType === "android") {// 赋值到附加字段(格式化为json)extra = JSON.parse(extra);}// 判断附加字段值if (extra.aa) { // 极光推送后台中添加的附加字段// 你的逻辑代码}}
}
这篇关于apicloud极光推送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!