PushMessageConfig.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package com.fdkankan.modeling.push;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.modeling.push.android.*;
  6. import com.fdkankan.modeling.push.ios.*;
  7. import lombok.extern.slf4j.Slf4j;
  8. import java.util.Date;
  9. @Slf4j
  10. public class PushMessageConfig {
  11. private String appkey = null;
  12. private String appMasterSecret = null;
  13. private String timestamp = null;
  14. private PushClient client = new PushClient();
  15. public static final String ANDROID_KEY = "5ee71c03dbc2ec076dd488cb";
  16. public static final String ANDROID_SECRET = "gzw4r8frbhq6eigxrvgjkdrm7wgtu83g";
  17. public static final String IOS_KEY = "5ee71ca9978eea081640f22a";
  18. public static final String IOS_SECRET = "tpuhyojch16pcha2qmpidlbnzkielv9w";
  19. //转台双目使用
  20. public static final String ANDROID_KEY_Z = "60efd777a6f90557b7b97c25";
  21. public static final String ANDROID_SECRET_Z = "ll85ov3qzeuas0ig7cw0v5bgzq1bdl07";
  22. public static final String IOS_KEY_Z = "60efd7c9a6f90557b7b97d10";
  23. public static final String IOS_SECRET_Z = "li62b5f3d9kn8idvy6qva3c5gyidmqwl";
  24. public PushMessageConfig(String key, String secret) {
  25. try {
  26. appkey = key;
  27. appMasterSecret = secret;
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. System.exit(1);
  31. }
  32. }
  33. public void sendAndroidBroadcast() throws Exception {
  34. AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
  35. broadcast.setTicker( "Android broadcast ticker");
  36. broadcast.setTitle( "中文的title");
  37. broadcast.setText( "Android broadcast text");
  38. broadcast.goAppAfterOpen();
  39. broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  40. // TODO Set 'production_mode' to 'false' if it's a test device.
  41. // For how to register a test device, please see the developer doc.
  42. broadcast.setProductionMode();
  43. // Set customized fields
  44. broadcast.setExtraField("test", "helloworld");
  45. //厂商通道相关参数
  46. broadcast.setChannelActivity("your channel activity");
  47. broadcast.setChannelProperties("abc");
  48. client.send(broadcast);
  49. }
  50. public void sendAndroidUnicast(String token, String ticker, String title, String text, String url) throws Exception {
  51. AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
  52. // TODO Set your device token
  53. unicast.setDeviceToken(token);
  54. unicast.setTicker(ticker);
  55. unicast.setTitle(title);
  56. unicast.setText(text);
  57. unicast.goUrlAfterOpen(url);
  58. unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  59. // TODO Set 'production_mode' to 'false' if it's a test device.
  60. // For how to register a test device, please see the developer doc.
  61. unicast.setProductionMode();
  62. // Set customized fields
  63. unicast.setExtraField("test", "helloworld");
  64. unicast.setChannelActivity("com.fdage.eight.module.EightPushMsgHandlerActivity");
  65. // unicast.setChannelProperties("abc");
  66. client.send(unicast);
  67. }
  68. //转台相机
  69. public void sendAndroidUnicast2(String token, String ticker, String title, String text, String url) throws Exception {
  70. AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
  71. // TODO Set your device token
  72. unicast.setDeviceToken(token);
  73. unicast.setTicker(ticker);
  74. unicast.setTitle(title);
  75. unicast.setText(text);
  76. unicast.goUrlAfterOpen(url);
  77. unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  78. // TODO Set 'production_mode' to 'false' if it's a test device.
  79. // For how to register a test device, please see the developer doc.
  80. unicast.setProductionMode();
  81. // Set customized fields
  82. unicast.setExtraField("test", "helloworld");
  83. unicast.setChannelActivity("io.github.zileyuan.umeng_analytics_push.OfflineNotifyClickActivity");
  84. // unicast.setChannelProperties("abc");
  85. client.send(unicast);
  86. }
  87. public void sendAndroidGroupcast() throws Exception {
  88. AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
  89. /* TODO
  90. * Construct the filter condition:
  91. * "where":
  92. * {
  93. * "and":
  94. * [
  95. * {"tag":"test"},
  96. * {"tag":"Test"}
  97. * ]
  98. * }
  99. */
  100. JSONObject filterJson = new JSONObject();
  101. JSONObject whereJson = new JSONObject();
  102. JSONArray tagArray = new JSONArray();
  103. JSONObject testTag = new JSONObject();
  104. JSONObject TestTag = new JSONObject();
  105. testTag.put("tag", "test");
  106. TestTag.put("tag", "Test");
  107. tagArray.add(testTag);
  108. tagArray.add(TestTag);
  109. whereJson.put("and", tagArray);
  110. filterJson.put("where", whereJson);
  111. groupcast.setFilter(filterJson);
  112. groupcast.setTicker( "Android groupcast ticker");
  113. groupcast.setTitle( "中文的title");
  114. groupcast.setText( "Android groupcast text");
  115. groupcast.goAppAfterOpen();
  116. groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  117. groupcast.setChannelActivity("your channel activity");
  118. // TODO Set 'production_mode' to 'false' if it's a test device.
  119. // For how to register a test device, please see the developer doc.
  120. groupcast.setProductionMode();
  121. //厂商通道相关参数
  122. groupcast.setChannelActivity("your channel activity");
  123. groupcast.setChannelProperties("abc");
  124. client.send(groupcast);
  125. }
  126. public void sendAndroidCustomizedcast() throws Exception {
  127. AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
  128. // TODO Set your alias here, and use comma to split them if there are multiple alias.
  129. // And if you have many alias, you can also upload a file containing these alias, then
  130. // use file_id to send customized notification.
  131. customizedcast.setAlias("alias", "alias_type");
  132. customizedcast.setTicker( "Android customizedcast ticker");
  133. customizedcast.setTitle( "中文的title");
  134. customizedcast.setText( "Android customizedcast text");
  135. customizedcast.goAppAfterOpen();
  136. customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  137. // TODO Set 'production_mode' to 'false' if it's a test device.
  138. // For how to register a test device, please see the developer doc.
  139. customizedcast.setProductionMode();
  140. //厂商通道相关参数
  141. customizedcast.setChannelActivity("your channel activity");
  142. customizedcast.setChannelProperties("abc");
  143. client.send(customizedcast);
  144. }
  145. public void sendAndroidCustomizedcastFile() throws Exception {
  146. AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
  147. // TODO Set your alias here, and use comma to split them if there are multiple alias.
  148. // And if you have many alias, you can also upload a file containing these alias, then
  149. // use file_id to send customized notification.
  150. String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb"+"\n"+"alias");
  151. customizedcast.setFileId(fileId, "alias_type");
  152. customizedcast.setTicker( "Android customizedcast ticker");
  153. customizedcast.setTitle( "中文的title");
  154. customizedcast.setText( "Android customizedcast text");
  155. customizedcast.goAppAfterOpen();
  156. customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  157. // TODO Set 'production_mode' to 'false' if it's a test device.
  158. // For how to register a test device, please see the developer doc.
  159. customizedcast.setProductionMode();
  160. //厂商通道相关参数
  161. customizedcast.setChannelActivity("your channel activity");
  162. customizedcast.setChannelProperties("abc");
  163. client.send(customizedcast);
  164. }
  165. public void sendAndroidFilecast() throws Exception {
  166. AndroidFilecast filecast = new AndroidFilecast(appkey,appMasterSecret);
  167. // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
  168. String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
  169. filecast.setFileId( fileId);
  170. filecast.setTicker( "Android filecast ticker");
  171. filecast.setTitle( "中文的title");
  172. filecast.setText( "Android filecast text");
  173. filecast.goAppAfterOpen();
  174. filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
  175. //厂商通道相关参数
  176. filecast.setChannelActivity("your channel activity");
  177. filecast.setChannelProperties("abc");
  178. client.send(filecast);
  179. }
  180. public void sendIOSBroadcast() throws Exception {
  181. IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
  182. //alert值设置为字符串
  183. //broadcast.setAlert("IOS 广播测试");
  184. //alert的值设置为字典
  185. // broadcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
  186. broadcast.setBadge( 0);
  187. broadcast.setSound( "default");
  188. // TODO set 'production_mode' to 'true' if your app is under production mode
  189. broadcast.setTestMode();
  190. // Set customized fields
  191. broadcast.setCustomizedField("test", "helloworld");
  192. client.send(broadcast);
  193. }
  194. public void sendIOSUnicast(String token, String title ,String subtitle , String body, String url) throws Exception {
  195. IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
  196. // TODO Set your device token
  197. unicast.setDeviceToken(token);
  198. //alert值设置为字符串
  199. //unicast.setAlert("IOS 单播测试");
  200. //alert的值设置为字典
  201. unicast.setAlert(title, subtitle, body, url);
  202. // unicast.setBadge( 0);
  203. unicast.setSound( "default");
  204. // TODO set 'production_mode' to 'true' if your app is under production mode
  205. unicast.setProductionMode();
  206. // unicast.setTestMode();
  207. unicast.setDescription(title);
  208. unicast.setExpireTime(DateUtil.formatTime(DateUtil.offsetDay(new Date(), 1)));
  209. // Set customized fields
  210. // unicast.setCustomizedField("url", url);
  211. client.send(unicast);
  212. }
  213. public void sendIOSGroupcast() throws Exception {
  214. IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
  215. /* TODO
  216. * Construct the filter condition:
  217. * "where":
  218. * {
  219. * "and":
  220. * [
  221. * {"tag":"iostest"}
  222. * ]
  223. * }
  224. */
  225. JSONObject filterJson = new JSONObject();
  226. JSONObject whereJson = new JSONObject();
  227. JSONArray tagArray = new JSONArray();
  228. JSONObject testTag = new JSONObject();
  229. testTag.put("tag", "iostest");
  230. tagArray.add(testTag);
  231. whereJson.put("and", tagArray);
  232. filterJson.put("where", whereJson);
  233. log.info(filterJson.toString());
  234. // Set filter condition into rootJson
  235. groupcast.setFilter(filterJson);
  236. //groupcast.setAlert("IOS 组播测试");
  237. //alert的值设置为字典
  238. // groupcast.setAlert("今日天气" , "subtitle" , "今日可能下雨🌂");
  239. groupcast.setBadge( 0);
  240. groupcast.setSound( "default");
  241. // TODO set 'production_mode' to 'true' if your app is under production mode
  242. groupcast.setTestMode();
  243. client.send(groupcast);
  244. }
  245. public void sendIOSCustomizedcast() throws Exception {
  246. IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
  247. // TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
  248. // And if you have many alias, you can also upload a file containing these alias, then
  249. // use file_id to send customized notification.
  250. customizedcast.setAlias("alias", "alias_type");
  251. //customizedcast.setAlert("IOS 个性化测试");
  252. //alert的值设置为字典
  253. // customizedcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
  254. customizedcast.setBadge( 0);
  255. customizedcast.setSound( "default");
  256. // TODO set 'production_mode' to 'true' if your app is under production mode
  257. customizedcast.setTestMode();
  258. client.send(customizedcast);
  259. }
  260. public void sendIOSFilecast() throws Exception {
  261. IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
  262. // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
  263. String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
  264. filecast.setFileId( fileId);
  265. //filecast.setAlert("IOS 文件播测试");
  266. //alert的值设置为字典
  267. // filecast.setAlert("今日天气" , "" , "今日可能下雨🌂");
  268. filecast.setBadge( 0);
  269. filecast.setSound( "default");
  270. // TODO set 'production_mode' to 'true' if your app is under production mode
  271. filecast.setTestMode();
  272. client.send(filecast);
  273. }
  274. public static void main(String[] args) {
  275. // TODO set your appkey and master secret here
  276. //安卓
  277. // Demo demo = new Demo(ANDROID_KEY, ANDROID_SECRET);
  278. //ios
  279. PushMessageConfig pushMessageConfig = new PushMessageConfig(IOS_KEY, IOS_SECRET);
  280. try {
  281. // demo.sendAndroidUnicast("AneqkEZahjbW3cF7gu8juNYqz54ZFfK7kjMpTOiQL9dl",
  282. // "测试项目计算完成", "四维看看Pro", "您上传的测试项目计算完成,点击查看",
  283. // "https://test.4dkankan.com/smobile.html?m=t-e9uHHdn");
  284. pushMessageConfig.sendIOSUnicast("ec61a2aa52673c96c12024e07ce267e391ca560e0c60f15ee09e65c8843ef7f4",
  285. "四维看看Pro", "测试项目计算完成", "您上传的测试项目计算完成,点击查看",
  286. "https://test.4dkankan.com/smobile.html?m=t-e9uHHdn");
  287. /* TODO these methods are all available, just fill in some fields and do the test
  288. * demo.sendAndroidCustomizedcastFile();
  289. * demo.sendAndroidBroadcast();
  290. * demo.sendAndroidGroupcast();
  291. * demo.sendAndroidCustomizedcast();
  292. * demo.sendAndroidFilecast();
  293. *
  294. * demo.sendIOSBroadcast();
  295. * demo.sendIOSUnicast();
  296. * demo.sendIOSGroupcast();
  297. * demo.sendIOSCustomizedcast();
  298. * demo.sendIOSFilecast();
  299. */
  300. } catch (Exception ex) {
  301. ex.printStackTrace();
  302. }
  303. }
  304. }