C#端实现安卓APP推送功能

首先在极光推送上设置好相应信息。
接下来配置javaweb工程,maven依赖:

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
    <scope>providedscope>
dependency>
<dependency>
    <groupId>cn.jpush.apigroupId>
    <artifactId>jpush-clientartifactId>
    <version>3.2.17version>
dependency>

<dependency>
    <groupId>cn.jpush.apigroupId>
    <artifactId>jiguang-commonartifactId>
    <version>1.0.3version>
dependency>
<dependency>
   <groupId>io.nettygroupId>
   <artifactId>netty-allartifactId>
   <version>4.1.6.Finalversion>
   <scope>compilescope>
dependency>
<dependency>
   <groupId>com.google.code.gsongroupId>
   <artifactId>gsonartifactId>
   <version>2.3version>
dependency>
<dependency>
   <groupId>org.slf4jgroupId>
   <artifactId>slf4j-apiartifactId>
   <version>1.7.7version>
dependency>

编译器插件:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-compiler-pluginartifactId>
      <configuration>
        <source>1.8source>
        <target>1.8target>
        <encoding>utf-8encoding>
      configuration>
    plugin>
  plugins>
build>

创建Jpush类:

public class Jpush {
    public static void jpushAndroid(Map parm) {
           // 设置好账号的app_key和masterSecret 
           String appKey = "***";
           String masterSecret = "***";
           //创建JPushClient
           JPushClient jpushClient = new JPushClient(masterSecret, appKey);
           //推送的关键,构造一个payload 
           PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.all())//你项目中的所有用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //parm中存放推送内容
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

           try {
                PushResult pu = jpushClient.sendPush(payload);

            } catch (APIConnectionException e) {
                e.printStackTrace();
            } catch (APIRequestException e) {
                e.printStackTrace();
            }    
       }
}

创建servlet,以供外界调用,在post方法中调用Jpush的推送方法,map中存放推送信息。

    String tltle=request.getParameter("title");
    String body=request.getParameter("msg");
    Jpush jpush=new Jpush();
    Map<String,String> map=new HashMap();
    map.put("msg", msg);
    map.put("title", tltle);
    jpush.jpushAndroid(map);
    System.out.println("ok");

java服务器端部署完成,接下来是C#端。
先写一个方法:

 private static HttpWebResponse NewMethod(string url)
        {
            HttpWebRequest request = null;
            request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Method = "GET";
            return request.GetResponse() as HttpWebResponse;
        }

然后画一个按钮,

 private void button1_Click(object sender, EventArgs e){
     string url = "http://192.168.0.45:8082/jpush_test/Test?title="+title+"&msg="+msg;
     NewMethod(url);
 }

点击按钮,手机接收到推送消息。

你可能感兴趣的:(工作经验)