使用apicloud完成消息推送功能



 apicloud是一款用html5封装的手机端的一个东西,消息推送就是我们经常说的手机打开一个软件后,该软件会出现相应的消息显示在通知栏,例如这样

                                                             使用apicloud完成消息推送功能_第1张图片


怎么才能实现呢,其实完成下面几步就ok了

1、在极光推送网站( https://www.jpush.cn )注册帐号,并创建应用,获取APP_KEY

              使用apicloud完成消息推送功能_第2张图片

2、在应用的config.xml下写下




  helloworld
  世界你好
  SK1995
  
  
  
  
  
  
  
  
  
  
  
  
  
    
    
  


示例如下:

<feature name="ajpush">
        <param name="app_key" value="123456789" />
        <param name="channel" value="your channel" />
    feature>
1. app_key:通过极光推送网站获得
2. channel: 渠道号


渠道号随便写,没有关系


然后在页面加入启动就自动执行的方法,加上初始化和监听

function test() {
				var ajpush = api.require('ajpush');
				var param = {
					alias : 'myalias',
					tags : ['tag1', 'tag2']
				};
				ajpush.bindAliasAndTags(param, function(ret) {
					var statusCode = ret.statusCode;
				});
				ajpush.init(function(ret) {
					if (ret && ret.status) {
						//success
						ajpush.setListener(function(ret) {
							var id = ret.id;
							var title = ret.title;
							var content = ret.content;
							var extra = ret.extra;
							console.log("id=" + id + ",title=" + title + ",content=" + content + ",extra=" + extra);
						});
					}
				});
			}

Java中代码这么写,appkey依然是极光的appkey

package qh.zcy.test;

import cn.jpush.api.JPushClient;
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 Jiguang {
     static JPushClient jPushClient=null;
	 static String masterSecret="17d3ea252e685d947a2911ac";
	 //static String appKey="15d39c8316d86519244de74f";
	 static String appKey="15d39c8316d86519244de74f";
	 static {
		 jPushClient =new JPushClient(masterSecret, appKey);
		 //PushPayload payload = buildPushObject_all_all_alert();
	 }
	 
	
	 public static PushPayload buildPushObject_all_all_alert(){
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag("tag1")).setNotification(Notification.android("乐吧一日游", "title", null)).build();
	 }
}

@RequestMapping("/push")  
    public static String push(HttpServletResponse response) throws APIConnectionException, APIRequestException{  
		PushPayload payload = Jiguang.buildPushObject_all_all_alert();
		PushResult result = Jiguang.jPushClient.sendPush(payload);
		response.setContentType("text/html");  
        response.setCharacterEncoding("utf-8");  
        PrintWriter out=null;  
        try {  
            Map maps=new HashMap();
            maps.put("name", "推送成功");
            
            JSONArray array=JSONArray.fromObject(maps);
           // JSONObject obj = JSONObject.fromObject(app);
            out=response.getWriter();  
            out.print(array);
            
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }finally{
        	out.close();
        }
		return null;
    }
	

打开这个地址 http://192.168.199.120:8081/testMVC/push.zcy就能进行推送了


你可能感兴趣的:(apicloud)