notification.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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("android.support.v4.app.NotificationCompat");
  56. }
  57. if (this.Notification) {
  58. const Build = plus.android.importClass("android.os.Build");
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  60. //android8.0及以上需设置通知渠道才能显示通知
  61. //创建通知渠道
  62. const name = "聊天通知";
  63. const description = "";
  64. const channelId = "sticky_channel"; //渠道id
  65. // const importance = NotificationManager.IMPORTANCE_DEFAULT;//重要性级别
  66. // const importance = NotificationManager.IMPORTANCE_HIGH;//重要性级别
  67. const importance = NotificationManager.IMPORTANCE_HIGH; //重要性级别
  68. const NotificationChannel = plus.android.importClass("android.app.NotificationChannel");
  69. const mChannel = new NotificationChannel(channelId, name, importance);
  70. mChannel.setDescription(description); //渠道描述
  71. mChannel.enableLights(false); //是否显示通知指示灯
  72. mChannel.enableVibration(false); //是否振动
  73. nm.createNotificationChannel(mChannel); //创建通知渠道
  74. this.notifyManager = nm;
  75. this.mNotificationBuild = new this.Notification.Builder(main,channelId);
  76. } else {
  77. this.notifyManager = nm;
  78. this.mNotificationBuild = new this.Notification.Builder(main);
  79. }
  80. }
  81. };
  82. createNotification = (config) => {
  83. let { title, content, tickerTips, notifyId } = config;
  84. if (!this.mNotificationBuild || !this.notifyManager) {
  85. return;
  86. }
  87. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  88. title = title || this.defaultTitle;
  89. content = content || this.defaultContent;
  90. tickerTips = tickerTips || this.defaultTicker;
  91. this.mNotificationBuild.setContentTitle(title);
  92. this.mNotificationBuild.setContentText(content);
  93. this.mNotificationBuild.setTicker(tickerTips);
  94. //默认有声音
  95. this.mNotificationBuild.setDefaults(this.Notification.DEFAULT_SOUND);
  96. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  97. this.mNotificationBuild.setPriority(this.Notification.PRIORITY_MAX);
  98. };
  99. createProgress = (config) => {
  100. let {
  101. progress,
  102. title,
  103. content,
  104. tickerTips,
  105. notifyId,
  106. intentList,
  107. isPause,
  108. } = config;
  109. if (!this.mNotificationBuild || !this.notifyManager) {
  110. return;
  111. }
  112. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  113. title = title || "APP更新包";
  114. content = content || `正在下载...${progress}%`;
  115. tickerTips = tickerTips || "进度提示";
  116. this.mNotificationBuild.setContentTitle(title);
  117. this.mNotificationBuild.setContentText(content);
  118. this.mNotificationBuild.setTicker(tickerTips);
  119. const R = plus.android.importClass("android.R");
  120. this.mNotificationBuild.setSmallIcon(
  121. isPause
  122. ? R.drawable.stat_sys_download_done
  123. : R.drawable.stat_sys_download,
  124. );
  125. if (intentList) {
  126. const pendingIntent = this.createIntent(notifyId, intentList);
  127. this.mNotificationBuild.setContentIntent(pendingIntent);
  128. }
  129. /*
  130. 如果为确定的进度条:调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0, 0, false)既可。
  131. 如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0, 0, true) ,操作结束时,调用setProgress(0, 0, false)并更新通知以移除指示条
  132. */
  133. //进度条显示时,默认无声音
  134. this.mNotificationBuild.setDefaults(0);
  135. this.mNotificationBuild.setProgress(100, progress, false);
  136. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  137. };
  138. compProgressNotification = (config) => {
  139. let { title, content, notifyId, intentList } = config;
  140. if (!this.mNotificationBuild || !this.notifyManager) {
  141. return;
  142. }
  143. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  144. title = title || "APP更新包";
  145. content = content || "下载完毕!";
  146. const R = plus.android.importClass("android.R");
  147. this.mNotificationBuild.setSmallIcon(R.drawable.stat_sys_download_done);
  148. this.mNotificationBuild.setContentTitle(title);
  149. this.mNotificationBuild.setContentText(content);
  150. this.mNotificationBuild.setAutoCancel(true);
  151. this.mNotificationBuild.setProgress(0, 0, false); //移除进度条
  152. if (intentList) {
  153. const pendingIntent = this.createIntent(notifyId, intentList);
  154. this.mNotificationBuild.setContentIntent(pendingIntent);
  155. }
  156. //默认有声音
  157. // this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
  158. this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
  159. };
  160. clearNotification = (notifyId) => {
  161. notifyId = typeof notifyId == "number" ? notifyId : this.defaultNotifyId;
  162. if (this.notifyManager) {
  163. this.notifyManager.cancel(notifyId);
  164. }
  165. };
  166. clearAllNotification = () => {
  167. if (this.notifyManager) {
  168. this.notifyManager.cancelAll();
  169. }
  170. };
  171. createIntent = (notifyId, intentList) => {
  172. const main = plus.android.runtimeMainActivity();
  173. const Intent = plus.android.importClass("android.content.Intent");
  174. const PendingIntent = plus.android.importClass("android.app.PendingIntent");
  175. const intent = new Intent(main, main.getClass());
  176. intentList.map((item) => {
  177. intent.putExtra(item[0], item[1]);
  178. });
  179. return PendingIntent.getActivity(
  180. main,
  181. notifyId,
  182. intent,
  183. PendingIntent.FLAG_CANCEL_CURRENT,
  184. );
  185. };
  186. compareVersion = (oldVersion, nowVersion) => {
  187. if (!oldVersion || !nowVersion || oldVersion == "" || nowVersion == "") {
  188. return false;
  189. }
  190. const oldVersionA = oldVersion.split(".", 4);
  191. const nowVersionA = nowVersion.split(".", 4);
  192. for (const i = 0; i < oldVersionA.length && i < nowVersionA.length; i++) {
  193. const strOld = oldVersionA[i];
  194. const numOld = parseInt(strOld);
  195. const strNow = nowVersionA[i];
  196. const numNow = parseInt(strNow);
  197. if (numNow > numOld /*||strNow.length>strOld.length*/) {
  198. return true;
  199. } else if (numNow < numOld) {
  200. return false;
  201. }
  202. }
  203. if (
  204. nowVersionA.length > oldVersionA.length &&
  205. 0 == nowVersion.indexOf(oldVersion)
  206. ) {
  207. return true;
  208. }
  209. };
  210. }