您的位置:首页 > 其它

极光推送 Demo

2016-01-16 11:01 204 查看
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);

    }

      

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: