新版本极光推送

public class JPushUtils {
    
    //Portal上注册应用时生成的 masterSecret
    private static final String MASTER_SECTET = "";
    //Portal上注册应用时生成的 appKey
    private static final String APP_KEY = "";
    //从消息推送时起,保存离线的时长。秒为单位。最多支持10天(864000秒)。

    
    private static PushPayload pushNotifiton(String receiver_type ,String receiver_value,String content ,Map<String, String> extra,String title){
         extra.put("title", title);
         return PushPayload.newBuilder()
                    .setPlatform(Platform.all()) 
                    .setAudience(Audience.alias(receiver_value))
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                      .setTitle(title)
                                      .setAlert(content)
                                      .addExtras(extra).build())
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .incrBadge(1)
                                    .setBadge(0)
                                    .setSound("")
                                    .setAlert(content)
                                    .addExtras(extra).build())
                            .build()).build();
                    
    }

    /**
     * 向手机端推送通知
     * @param userids    用户id多个逗号隔开
     * @param content    推送内容
     * @param businessId    业务id,android客户端根据业务id判断是那个业务
     * @return
     */
    public static boolean pushNotifiton(String userids,String content ,Map<String,String> extra,String title){
        boolean flag=false;
        JPushClient jpushClient = new JPushClient(MASTER_SECTET, APP_KEY, 3);
        PushPayload payload=pushNotifiton("3",userids,content,extra,title);
         try {
                jpushClient.sendPush(payload);
                flag=true;
            } catch (Exception e) {
                flag=false;
            }
        return flag;
    }
    /**
     * 向手机端推送消息
     * @param userids    用户id多个逗号隔开
     * @param content    推送内容
     * @param businessId    业务id,android客户端根据业务id判断是那个业务
     * @return
     */
    public static boolean pushMsg(String userids,String content,Map<String,Object> extra){
        boolean flag=false;
         JPushClient jpushClient = new JPushClient(MASTER_SECTET, APP_KEY, 3);
         PushPayload payload = pushMsg("3",userids,content,extra);
         try {
            PushResult result = jpushClient.sendPush(payload);
            flag=true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            flag=false;
        }
         return flag;
    }
    /**
     * 向客户端发送消息方法
     * @param receiver_type        发送类型
     * @param receiver_value       接收人
     * @param content            消息内容
     * @param businessId        业务id
     */
    private static PushPayload pushMsg(String receiver_type ,String receiver_value,String content,Map<String,Object> extra){
          return PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.newBuilder()
                            .addAudienceTarget(AudienceTarget.alias(receiver_value))
                            .build())
                    .setMessage(Message.newBuilder()
                            .setMsgContent(content)
                            .addExtra("noticeId", extra.get("noticeId").toString())
                            .addExtra("type", extra.get("type").toString())
                            .build())
                    .build();
    }
    public static void main(String[] args) {  
        Map<String,String> extra = new LinkedHashMap<String,String>();
        
        //公告id
        extra.put("noticeId", "45678");
        
        extra.put("type", "s");
        extra.put("sendman", "我");
        Format format = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        extra.put("sendtime", format.format(new Date()));
        extra.put("content", "我看对");
        
        System.out.println(pushNotifiton("admi1n","cs1",extra,"cs2"));
    }
}

 

转载于:https://www.cnblogs.com/doublekiller/p/5650022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值