notification.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //设为true代表常驻状态栏
  2. // this.mNotificationBuild.setOngoing(false);
  3. //设置通知栏标题
  4. // this.mNotificationBuild.setContentTitle(defaultTitle);
  5. //设置通知栏显示内容
  6. // this.mNotificationBuild.setContentText(defaultContent);
  7. //设置通知栏点击意图
  8. // this.mNotificationBuild.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
  9. //通知首次出现在通知栏,带上升动画效果的
  10. // this.mNotificationBuild.setTicker(defaultTicker);
  11. //设置通知集合的数量
  12. // this.mNotificationBuild.setNumber(defaultNumber)
  13. //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
  14. // this.mNotificationBuild.setWhen(System.currentTimeMillis());
  15. //设置该通知优先级
  16. // this.mNotificationBuild.setPriority(Notification.PRIORITY_DEFAULT);
  17. //设置这个标志当用户单击面板就可以让通知将自动取消
  18. // this.mNotificationBuild.setAutoCancel(true);
  19. //向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
  20. //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND Notification.DEFAULT_VIBRATE
  21. // console.log('默认:'+plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
  22. // this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
  23. //默认的push图标
  24. // this.mNotificationBuild.setSmallIcon(17301620);//设置小图标
  25. //https://www.cnblogs.com/penghuster/p/4909930.html
  26. // const R = plus.android.importClass("android.R");
  27. // this.mNotificationBuild.setSmallIcon(R.drawable.stat_sys_download);
  28. export default class NotificationUtil {
  29. notifyManager;
  30. mNotificationBuild;
  31. Notification;
  32. defaultTitle = "通知栏标题";
  33. defaultContent = "通知内容";
  34. defaultTicker = "通知提示";
  35. defaultNotifyId = 1000;
  36. constructor() {
  37. this.initNotification();
  38. }
  39. initNotification = () => {
  40. if (plus.os.name != "Android") {
  41. return;
  42. }
  43. //当前版本号
  44. const SystemVersion = plus.os.version;
  45. const Context = plus.android.importClass("android.content.Context");
  46. const main = plus.android.runtimeMainActivity();
  47. const NotificationManager = plus.android.importClass(
  48. "android.app.NotificationManager",
  49. );
  50. const nm = main.getSystemService(Context.NOTIFICATION_SERVICE);
  51. // Notification build 要android api16以上才能使用(4.1.2以上)
  52. if (this.compareVersion("4.1.1", SystemVersion) == true) {
  53. this.Notification = plus.android.importClass("android.app.Notification");
  54. } else {
  55. this.Notification = plus.android.importClass(
  56. "android.support.v4.app.NotificationCompat",
  57. );
  58. }
  59. if (this.Notification) {
  60. const Build = plus.android.importClass("android.os.Build");
  61. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  62. //android8.0及以上需设置通知渠道才能显示通知
  63. //创建通知渠道
  64. const name = "渠道名称1";
  65. const description = "渠道描述1";
  66. const channelId = "channelId1"; //渠道id
  67. // const importance = NotificationManager.IMPORTANCE_DEFAULT;//重要性级别
  68. // const importance = NotificationManager.IMPORTANCE_HIGH;//重要性级别
  69. const importance = NotificationManager.IMPORTANCE_LOW; //重要性级别
  70. const NotificationChannel = plus.android.importClass(
  71. "android.app.NotificationChannel",
  72. );
  73. const mChannel = new NotificationChannel(channelId, name, importance);
  74. mChannel.setDescription(description); //渠道描述
  75. mChannel.setDescription("渠道描述1"); //渠道描述
  76. mChannel.enableLights(false); //是否显示通知指示灯
  77. mChannel.enableVibration(false); //是否振动
  78. nm.createNotificationChannel(mChannel); //创建通知渠道
  79. this.notifyManager = nm;
  80. this.mNotificationBuild = new this.Notification.Builder(
  81. main,
  82. channelId,
  83. );
  84. } else {
  85. this.notifyManager = nm;
  86. this.mNotificationBuild = new this.Notification.Builder(main);
  87. }
  88. }
  89. };
  90. createNotification = (config) => {
  91. let { title, content, tickerTips, notifyId } = config;
  92. if (!this.mNotificationBuild || !this.notifyManager) {
  93. return;
  94. }
  95. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  96. title = title || this.defaultTitle;
  97. content = content || this.defaultContent;
  98. tickerTips = tickerTips || this.defaultTicker;
  99. this.mNotificationBuild.setContentTitle(title);
  100. this.mNotificationBuild.setContentText(content);
  101. this.mNotificationBuild.setTicker(tickerTips);
  102. //默认有声音
  103. this.mNotificationBuild.setDefaults(this.Notification.DEFAULT_SOUND);
  104. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  105. };
  106. createProgress = (config) => {
  107. let {
  108. progress,
  109. title,
  110. content,
  111. tickerTips,
  112. notifyId,
  113. intentList,
  114. isPause,
  115. } = config;
  116. if (!this.mNotificationBuild || !this.notifyManager) {
  117. return;
  118. }
  119. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  120. title = title || "APP更新包";
  121. content = content || `正在下载...${progress}%`;
  122. tickerTips = tickerTips || "进度提示";
  123. this.mNotificationBuild.setContentTitle(title);
  124. this.mNotificationBuild.setContentText(content);
  125. this.mNotificationBuild.setTicker(tickerTips);
  126. const R = plus.android.importClass("android.R");
  127. this.mNotificationBuild.setSmallIcon(
  128. isPause
  129. ? R.drawable.stat_sys_download_done
  130. : R.drawable.stat_sys_download,
  131. );
  132. if (intentList) {
  133. const pendingIntent = this.createIntent(notifyId, intentList);
  134. this.mNotificationBuild.setContentIntent(pendingIntent);
  135. }
  136. /*
  137. 如果为确定的进度条:调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0, 0, false)既可。
  138. 如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0, 0, true) ,操作结束时,调用setProgress(0, 0, false)并更新通知以移除指示条
  139. */
  140. //进度条显示时,默认无声音
  141. this.mNotificationBuild.setDefaults(0);
  142. this.mNotificationBuild.setProgress(100, progress, false);
  143. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  144. };
  145. compProgressNotification = (config) => {
  146. let { title, content, notifyId, intentList } = config;
  147. if (!this.mNotificationBuild || !this.notifyManager) {
  148. return;
  149. }
  150. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  151. title = title || "APP更新包";
  152. content = content || "下载完毕!";
  153. const R = plus.android.importClass("android.R");
  154. this.mNotificationBuild.setSmallIcon(R.drawable.stat_sys_download_done);
  155. this.mNotificationBuild.setContentTitle(title);
  156. this.mNotificationBuild.setContentText(content);
  157. this.mNotificationBuild.setAutoCancel(true);
  158. this.mNotificationBuild.setProgress(0, 0, false); //移除进度条
  159. if (intentList) {
  160. const pendingIntent = this.createIntent(notifyId, intentList);
  161. this.mNotificationBuild.setContentIntent(pendingIntent);
  162. }
  163. //默认有声音
  164. // this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
  165. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  166. };
  167. clearNotification = (notifyId) => {
  168. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  169. if (this.notifyManager) {
  170. this.notifyManager.cancel(notifyId);
  171. }
  172. };
  173. clearAllNotification = () => {
  174. if (this.notifyManager) {
  175. this.notifyManager.cancelAll();
  176. }
  177. };
  178. createIntent = (notifyId, intentList) => {
  179. const main = plus.android.runtimeMainActivity();
  180. const Intent = plus.android.importClass("android.content.Intent");
  181. const PendingIntent = plus.android.importClass("android.app.PendingIntent");
  182. const intent = new Intent(main, main.getClass());
  183. intentList.map((item) => {
  184. intent.putExtra(item[0], item[1]);
  185. });
  186. return PendingIntent.getActivity(
  187. main,
  188. notifyId,
  189. intent,
  190. PendingIntent.FLAG_CANCEL_CURRENT,
  191. );
  192. };
  193. compareVersion = (oldVersion, nowVersion) => {
  194. if (!oldVersion || !nowVersion || oldVersion == "" || nowVersion == "") {
  195. return false;
  196. }
  197. const oldVersionA = oldVersion.split(".", 4);
  198. const nowVersionA = nowVersion.split(".", 4);
  199. for (const i = 0; i < oldVersionA.length && i < nowVersionA.length; i++) {
  200. const strOld = oldVersionA[i];
  201. const numOld = parseInt(strOld);
  202. const strNow = nowVersionA[i];
  203. const numNow = parseInt(strNow);
  204. if (numNow > numOld /*||strNow.length>strOld.length*/) {
  205. return true;
  206. } else if (numNow < numOld) {
  207. return false;
  208. }
  209. }
  210. if (
  211. nowVersionA.length > oldVersionA.length &&
  212. 0 == nowVersion.indexOf(oldVersion)
  213. ) {
  214. return true;
  215. }
  216. };
  217. }