集成极光推送服务,实现通知推送功能
当下有个产品需要给移动端(安卓和ios)推送消息通知,个人觉得消息推送功能如何有条件的话还是别依赖于第三方的服务。
因为消息由第三方软件公司去发送首先安全性就没有保障了,其次是对数据的处理,尽管很多第三方服务都提供了很多的api接口,但是总有一些需求是这种第三方服务满足不了的,所以有条件的公司可以自己搭一套消息推送服务。好了废话不多说,先讲一下极光推送这个第三方的服务吧,总体来讲还是比较专业的,官网也给出了比较简单的demo,但是也仅限于简单的demo,如果想传递带附加消息的通知你在官网是看不到的,找了半天才在别人的博客上找到,所以这里晒一下我使用极光推送用到的帮助类。
1 package cfs.wsdl.jpush; 2 3 import java.sql.Timestamp; 4 import java.text.SimpleDateFormat; 5 import java.util.ArrayList; 6 import java.util.Date; 7 import java.util.List; 8 import java.util.UUID; 9 import java.util.regex.Pattern; 10 11 import com.google.gson.JsonElement; 12 import com.google.gson.JsonObject; 13 14 import cfs.core.constant.CfsConstant; 15 import cfs.core.util.Util; 16 import cfs.wsdl.cache.Mycache; 17 import cfs.wsdl.constant.MobileConstant; 18 import cfs.wsdl.model.AnswerQuestion; 19 import cfs.wsdl.model.Questions; 20 import cfs.wsdl.model.QuestionsBank; 21 import cfs.wsdl.model.Ranking; 22 import cn.jpush.api.JPushClient; 23 import cn.jpush.api.common.resp.APIConnectionException; 24 import cn.jpush.api.common.resp.APIRequestException; 25 import cn.jpush.api.push.PushResult; 26 import cn.jpush.api.push.model.Message; 27 import cn.jpush.api.push.model.Options; 28 import cn.jpush.api.push.model.Platform; 29 import cn.jpush.api.push.model.PushPayload; 30 import cn.jpush.api.push.model.audience.Audience; 31 import cn.jpush.api.push.model.notification.AndroidNotification; 32 import cn.jpush.api.push.model.notification.IosNotification; 33 import cn.jpush.api.push.model.notification.Notification; 34 35 public class JpushUtil { 36 37 //教师端 38 public static final String mastersecret_teacher="51411********"; 39 public static final String appkey_teacher="f85d6231*********"; 40 public static final String content_teacher="学生们提交作业啦,快去看看吧。"; 41 42 43 public static void main(String[] args) { 44 JsonObject j= new JsonObject(); 45 j.addProperty("homework_name", "随便写的"); 46 j.addProperty("assignmen_id", "bc58e262-6b27-477f-a605-7e1b1318e0dd"); 47 JpushUtil ju=new JpushUtil(); 48 ju.jpushSend(JpushUtil.mastersecret_teacher,JpushUtil.appkey_teacher,"单独给小伟发个测试用的数据",j); 49 } 50 51 52 53 public void jpushSend(String mastersecret,String appkey, String content,JsonObject extra){ 54 JPushClient jpushClient = new JPushClient(mastersecret, appkey); 55 PushPayload payload =PushPayload.newBuilder() 56 .setPlatform(Platform.all()) 57 // .setAudience(Audience.tag_and("tag1", "tag_all")) 58 .setAudience(Audience.all()) 59 .setNotification(Notification.newBuilder() 60 //设置安卓平台消息推送 61 .addPlatformNotification(AndroidNotification.newBuilder().setAlert(content) 62 .addExtra("param", extra) 63 .build()) 64 //设置ios平台消息推送 65 .addPlatformNotification(IosNotification.newBuilder() 66 .setAlert(content) 67 .autoBadge() 68 // .setBadge(5) 69 // .setSound("happy") 70 .addExtra("param", extra) 71 .build()) 72 .build()) 73 // .setMessage(Message.content(content)) 74 .setOptions(Options.newBuilder() 75 .setApnsProduction(false) 76 .build()) 77 .build(); 78 try { 79 PushResult result = jpushClient.sendPush(payload); 80 System.out.println("消息发送成功 - " + result); 81 } catch (APIConnectionException e) { 82 System.out.println("Connection error, should retry later========"+ e); 83 } catch (APIRequestException e) { 84 System.out.println("Should review the error, and fix the request========"+ e); 85 System.out.println("HTTP Status: " + e.getStatus()); 86 System.out.println("Error Code: " + e.getErrorCode()); 87 System.out.println("Error Message: " + e.getErrorMessage()); 88 } 89 } 90 91 }