# Android 极光推送服务器端和移动端

Android 极光推送服务器端和移动端

这里是Android 有关服务器端进行推送和手机客户端接收的一些简单的例子
服务器端采用的是MyEclipse2014,手机端是Android studio 2.0

下面是一个简单的JSP页面代码

<body>
   <form action="MyJpushServlset" method="GET">
    输入标题<input type="text" name="title">
<br />
    输入内容<input type="text" name="content" />
<br/>
    设备号码<input type="text" name="alias" />
<br/>
<select name="select" >
    <option value="type">选择推送类型</option>
<option value="type1">推送给所有用户</option>
<option value="type2">推送给指定用户</option>
    </select>

<input type="submit" value="推送" />
</form>
  </body>

这是服务器端封装的工具类

/**
 * 这里是极光推送服务器端的代码
 * @author ZPH
 * @
 * */
public class MyJpushUtil {
    private static final String appKey = "你的appKey";
    private static final String masterSecret = "你的masterSecret ";
    public static JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
    /**
     * 
     * 发送给所有的Android端设备(只要是审核通过,并且在客户端登录成功的用户都可以收到该条短信)
     * @param title,content
     * 
     * */

    public static void SendToAllUserMessage(String title,String content){
        PushPayload payload = buildPushObject_all_alias_alert(title,content);
        try {
            PushResult result = jpushClient.sendPush(payload);
            if(result.isResultOK()){
                System.out.println("消息推送成功......");
            }
            else
                System.out.println("消息推送失败......");

        } catch (APIConnectionException e) {
            System.out.println("消息给所有用户推送失败,APIConnectionException异常");
        } catch (APIRequestException e) {
            System.out.println("消息给所有用户推送失败,APIRequestException异常");
            e.printStackTrace();
        }


    }
    /**
     * 推送到所有的平台的返回值 PushPayload
     * */
    private static  PushPayload buildPushObject_all_alias_alert(String title,String content) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台
                .setAudience(Audience.all())// Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
                .setNotification(Notification.android(content, title, null)).build();
    }


    /**
     * 
     * 手机号作为设备的指定标记
     * 根据设备号(手机号)推送给指定的用户
     * @param title,content,phonenumber
     * */
    public static void SendToOneUserMessage(String title,String content,String phonenumber){
        PushPayload payload = buildPushObject_android_tag_alertWithTitle(title,content,phonenumber);

        try {
            PushResult result = jpushClient.sendPush(payload);
            if(result.isResultOK()){
                System.out.println("消息推送成功......");
            }
            else
                System.out.println("消息推送失败......");

        } catch (APIConnectionException e) {
            System.out.println("消息给所有用户推送失败,APIConnectionException异常");
            e.printStackTrace();
        } catch (APIRequestException e) {
            System.out.println("消息给所有用户推送失败,APIRequestException异常");
            e.printStackTrace();
        }


    }

    /**
     * 推送到指定用户的返回值 PushPayload
     * */
    private static PushPayload buildPushObject_android_tag_alertWithTitle(
            String title, String content,String phonenumber) {
        return PushPayload.newBuilder().setPlatform(Platform.android())
                .setAudience(Audience.alias(phonenumber))
                .setNotification(Notification.android(content, title, null))
                .build();
    }

}

然后通过servlet进行获取输入内容和简单调用

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title=new String(request.getParameter("title").getBytes("iso-8859-1"),"utf-8");;//从 request 中获取名为 title 的参数的值
        String content=new String(request.getParameter("content").getBytes("iso-8859-1"),"utf-8");//从 request 中获取名为 content 的参数的值
        String phonenumber=new String(request.getParameter("alias").getBytes("iso-8859-1"),"utf-8");//获取设备信息
        //      ToJpushAndroid(title,content,alias);

        String type=new String(request.getParameter("select").getBytes("iso-8859-1"),"utf-8");
        System.out.println("推送的标题是:"+title+"\n推送的内容是:"+content+
                "\n推送的手机端号码是:"+phonenumber);
        if(type.equals("type2")){
            //给指定的用户
            System.out.println("推送类型是:推送给指定用户");
            MyJpushUtil.SendToOneUserMessage(title, content, phonenumber);

        }
        else{
            //给所有的用户
            System.out.println("推送类型是:推送给所有用户");
            MyJpushUtil.SendToAllUserMessage(title, content);
        }

    }

所需要的jar包网上可以下载,这里就不给链接下载了。
以上是jpush的服务器端

下面是我们的android端进行的编写
Android端进行JPush环境的搭建可按照官网说明进行
这里我只是把用户手机号作为接受信息的唯一标示

 JPushInterface.setAlias(LoginActivity.this, phonenumber, new TagAliasCallback() {
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {

                        }
                    });

通过以上就可以进行服务器端和客户端进行交互了

猜你喜欢

转载自blog.csdn.net/qq_24536171/article/details/53817190
今日推荐