# Android Jiguang push server and mobile

Android Jiguang push server and mobile

Here are some simple examples of Android
server

Below is a simple JSP page code

<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>

This is a tool class encapsulated on the server side

/**
 * 这里是极光推送服务器端的代码
 * @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();
    }

}

Then get the input content and simple call through the 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);
        }

    }

The required jar package can be downloaded online, and the link to download is not given here.
The above is the server side of jpush

The following is our android side to write the
Android side to build the JPush environment, which can be done according to the official website instructions.
Here I just use the user's mobile phone number as the only indicator for receiving information

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

                        }
                    });

Through the above, the server side and the client side can interact

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325420472&siteId=291194637