概述
/**
* 极光消息推送(sendJGPush)
* @author 3hcxd
* @param type 推送方式(alias别名 registrationID注册ID tag标签 all广播)
* @param parm 推送内容参数(msg、account、title)
* @param regID 推送ID(取决于前端使用推送方式产生的推送ID)
* */
public static void sendJGPush(String type,Map<String, String> parm,String regID) {
String jpush_master_secret = "极光秘钥"; //app密钥 创建应用后生成(对应极光账号创建的应用秘钥)
String jpush_app_key = "极光Key";//appKey 创建应用后生成(对应极光账号创建的应用ID)
JPushClient jpushClient = new JPushClient(jpush_master_secret, jpush_app_key);
PushPayload payload = buildPushPayload(type, parm,regID); //极光推送消息体
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println(result.error);
System.out.println(result.statusCode);
System.out.println(result.msg_id);
System.out.println(result.sendno);
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
jpushClient.close();
} catch (APIConnectionException e) {
System.out.println("APIConnectionException");
e.printStackTrace();
} catch (APIRequestException e) {
System.out.println("APIRequestException");
e.printStackTrace();
}
}
/**
* 生成极光推送对象PushPayload
* @param type
* @param regID
* @param parm
* @return PushPayload
*/
public static PushPayload buildPushPayload(String type,Map<String, String> parm,String regID){
String msg = parm.get("msg");
String title = parm.get("title");
Map<String, String> extras = new HashMap<>();
extras.put("Account", parm.get("account"));
Audience audience = null;
switch (type) {
case "registrationID":
audience = Audience.registrationId(regID);//指定设备标识的用户
break;
case "alias":
audience = Audience.alias(regID);//使用别名标识的用户
break;
case "tag":
audience = Audience.tag(regID);//使用标签标识的用户
break;
default:
audience = Audience.all(); //所有用户
break;
}
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(audience)
.setOptions(Options.sendno())
.setNotification(Notification.android(msg, title, extras))
.build();
}
public static void main(String[] args) {
String type = "alias"; //此系统使用别名作为推送方式
Map<String, String> parm = new HashMap<>();
String content = "收到请及时处理";
parm.put("msg",content);
parm.put("account","testAccountID");
parm.put("title","新待办通知");
String regId = "2c9223876961c2d3016979c169760dd1"; //(此系统使用系统用户ID作为别名推送,前端注意判断)
sendJGPush(type,parm,regId);
}
最后
以上就是坦率云朵为你收集整理的极光推送java后端代码示例的全部内容,希望文章能够帮你解决极光推送java后端代码示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复