极光推送 Demo

package Jpush;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONObject;
import org.junit.Test;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.model.Message;
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 Demo {
    private static final String appKey = "注册获得:XXX";
      private static final String masterSecret = "注册获得:XXX";
      private String title = "title"; //标题
      private String content = "content"; //内容
    /**
     * 测试方法
     *
     */
    @Test
    public void doTest(){
        //pushAll();
        pushByAlias();
    }
    
    /**
     *发送推送
     * @param ppd
     */
    public String sendPush(PushPayload ppd){
        JSONObject result = new JSONObject();
        JPushClient jpushClient = new JPushClient(masterSecret,appKey);//注意参数顺序
        
        try {
            String json = jpushClient.sendPush(ppd).toString();
            if (json.startsWith("{")) {
                result.put("Android", new JSONObject(json));
            } else {
                result.put("Android", json);
            }
        } catch (APIConnectionException e) {  
            result.put("FAIL", "APIConnectionException"+e.getMessage());
        } catch (APIRequestException e) {
            result.put("FAIL", "APIRequestException:"+e.getErrorMessage()+",HTTP Status:"+e.getStatus());
        }  
        System.out.println(result.toString());        
        return result.toString();
        
      }
    //广播
    public String pushAll() {
        //PushPayload ppd =PushPayload.alertAll(title);
        
        //参数
        Map<String,String> map = new HashMap<String,String>();
        map.put("key", "value");
        
        //接受者
        Set<String> list = new HashSet<String>();
        list.add("b957fbe5e33c416db0942a489ec4f2db");
        list.add("2da32cc48f084d5c98d0d123e68dcaa9");
        
        PushPayload ppd = PushPayload.newBuilder()
                .setPlatform(Platform.all())        //设置平台
                .setAudience(Audience.all())        //设置接受者
                .setNotification(Notification.android(content, title, null)) //设置通知
                .setMessage(Message.content(content))                    //设置消息
                .build();
        
        return sendPush(ppd);
    }
    public  String pushByAlias() {
        //接受者 如果jpush中Alias一个都不存在,会报错
        Set<String> list = new HashSet<String>();
        list.add("b957fbe5e33c416db0942a489ec4f2db");
        list.add("2da32cc48f084d5c98d0d123e68dcaa9");
        //参数
        Map<String,String> map = new HashMap<String,String>();
        map.put("key", "value");
        PushPayload ppd = PushPayload.newBuilder()
                .setPlatform(Platform.all())        //设置平台
                .setAudience(Audience.alias(list))        //设置接受者
                .setNotification(Notification.android(content, title, map)) //设置通知
                .setMessage(Message.content(content))                    //设置消息
                .build();
        return sendPush(ppd);
    }
      
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值