使用apicloud完成消息推送功能

时间:2024-11-16 15:51:43



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

                                                            


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

1、在极光推送网站( )注册帐号,并创建应用,获取APP_KEY

             

2、在应用的下写下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <widget id="A6903547912689" version="0.0.1">
  3. <name>helloworld</name>
  4. <description>世界你好</description>
  5. <author email="834827635@" href="">SK1995</author>
  6. <content src=""/>
  7. <preference name="appBackground" value="rgba(0,0,0,0)"/>
  8. <preference name="windowBackground" value="rgba(0,0,0,0)"/>
  9. <preference name="frameBackgroundColor" value="rgba(0,0,0,0)"/>
  10. <preference name="autoLaunch" value="true"/>
  11. <preference name="autoUpdate" value="true"/>
  12. <preference name="smartUpdate" value="false"/>
  13. <preference name="debug" value="true"/>
  14. <preference name="statusBarAppearance" value="true"/>
  15. <permission name="location"/>
  16. <permission name="internet"/>
  17. <access origin="*"/>
  18. <feature name="ajpush">
  19. <param name="app_key" value="15d39c8316d86519244de74f"/>
  20. <param name="channel" value="17d3ea252e685d947a2911ac"/>
  21. </feature>
  22. </widget>


示例如下:

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


渠道号随便写,没有关系


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

  1. function test() {
  2. var ajpush = api.require('ajpush');
  3. var param = {
  4. alias : 'myalias',
  5. tags : ['tag1', 'tag2']
  6. };
  7. ajpush.bindAliasAndTags(param, function(ret) {
  8. var statusCode = ret.statusCode;
  9. });
  10. ajpush.init(function(ret) {
  11. if (ret && ret.status) {
  12. //success
  13. ajpush.setListener(function(ret) {
  14. var id = ret.id;
  15. var title = ret.title;
  16. var content = ret.content;
  17. var extra = ret.extra;
  18. console.log("hljs-string">",title=" + title + ",content=" + content + ",extra=" + extra);
  19. });
  20. }
  21. });
  22. }

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

  1. package ;
  2. import ;
  3. import ;
  4. import ;
  5. import ;
  6. import ;
  7. public class Jiguang {
  8. static JPushClient jPushClient=null;
  9. static String masterSecret="17d3ea252e685d947a2911ac";
  10. //static String appKey="15d39c8316d86519244de74f";
  11. static String appKey="15d39c8316d86519244de74f";
  12. static {
  13. jPushClient =new JPushClient(masterSecret, appKey);
  14. //PushPayload payload = buildPushObject_all_all_alert();
  15. }
  16. public static PushPayload buildPushObject_all_all_alert(){
  17. return ().setPlatform(()).setAudience(("tag1")).setNotification(("乐吧一日游", "title", null)).build();
  18. }
  19. }

  1. @RequestMapping("/push")
  2. public static String push(HttpServletResponse response) throws APIConnectionException, APIRequestException{
  3. PushPayload payload = Jiguang.buildPushObject_all_all_alert();
  4. PushResult result = (payload);
  5. ("text/html");
  6. ("utf-8");
  7. PrintWriter out=null;
  8. try {
  9. Map<String, String> maps=new HashMap<String, String>();
  10. ("name", "推送成功");
  11. JSONArray array=(maps);
  12. // JSONObject obj = (app);
  13. out=();
  14. (array);
  15. } catch (IOException e) {
  16. // TODO Auto-generated catch block
  17. ();
  18. }finally{
  19. ();
  20. }
  21. return null;
  22. }

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



相关文章