极光推送-工具类

 package com.huaxin.cloud.tianjian.util;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
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.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * Created by Chen Meng on 2017/9/29.
 */
public class JPushHelper {
    private static Properties P= null;
//    static {
//        try {
//            P=getP();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    //单例
    private static  JPushHelper jPushHelper=null;
    private  static JPushClient jPushClient=null;
    private JPushHelper(){
        jPushClient = new JPushClient("6cd1109d137ce735ae99883d","de17ee2a8c586fc764f18027");  //matersecret和appkey
    }
    public static synchronized JPushHelper getInstance(){
        if(jPushHelper==null){
            jPushHelper=new JPushHelper();
        }
        return jPushHelper;
    }
    //向单个用户发送通知
    public  static void sendPush(String alert,List<String> aliasList) throws APIConnectionException, APIRequestException, IOException {
        //JSONObject json = new JSONObject();
        //json.put("huaxin",map1);

   

 List<String> newList = new ArrayList<>();

    for(String item:aliasList) {
        newList.add("tj_"+item);
    }

//        Boolean APNSPRODUCTION=Boolean.parseBoolean(P.getProperty("ApnsProduction"));
        Map<String,String> map = new HashMap<String,String>();
        //安卓推送
        PushPayload  AZpushPayload= PushPayload.newBuilder()
                . setPlatform(Platform.android())       //推送的平台  Platform.ios()--ios平台  Platform.android()--安卓平台
                .setAudience(Audience.alias(newList))       //推送的对象
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)      //alert为推送内容
                                //*                              .setBadge(1)         //角标显示的数字*//*
                                //*  .setSound("happy")    //设置推送的声音*//*
                                .addExtras(map)       //附加字段
                                .build())
                        .build())
                .setMessage(Message.content(alert))  //推送内容是 - 内容为 MSG_CONTENT 的消息
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();

        //ios推送
        PushPayload  IospushPayload= PushPayload.newBuilder()
                . setPlatform(Platform.ios())       //推送的平台  Platform.ios()--ios平台  Platform.android()--安卓平台
                .setAudience(Audience.alias(newList))       //推送的对象
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)      //alert为推送内容
                                .setBadge(1)         //角标显示的数字
                                .setSound("happy")    //设置推送的声音
                                .addExtras(map)       //附加字段
                                .build())
                        .build())
                .setMessage(Message.content(alert))  //推送内容是 - 内容为 MSG_CONTENT 的消息
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();

        //同时推 安卓和ios  消息为自定义消息 安卓和IOS需改动 暂不启用
/*        PushPayload  pushPayload=PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(newList))
                .setMessage(Message.newBuilder()
                        .setMsgContent(alert)
                        .addExtra("from", "JPush")
                        .build())
                .build();*/

        try{
            PushResult AZresult = jPushClient.sendPush(AZpushPayload);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            PushResult Iosresult = jPushClient.sendPush(IospushPayload);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    //读取配置文件,来设置setApnsProduction(APNSPRODUCTION)的值
   //    public static Properties getP() throws IOException {
   //        ClassLoader classLoader = JPushHelper.class.getClassLoader();// 读取属性文件    xxxxx.properties
//        InputStream in = classLoader.getResourceAsStream("resources.properties");
//        Properties p = new Properties();
//        p.load(in);
//        in.close();
//        return p;
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值