uni-push.plus.es.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function initPushNotification() {
  2. // 仅 App 端
  3. if (typeof plus !== 'undefined' && plus.push) {
  4. plus.globalEvent.addEventListener('newPath', ({ path }) => {
  5. if (!path) {
  6. return;
  7. }
  8. // 指定的页面为当前页面
  9. const pages = getCurrentPages();
  10. const currentPage = pages[pages.length - 1];
  11. if (currentPage &&
  12. currentPage.$page &&
  13. currentPage.$page.fullPath === path) {
  14. return;
  15. }
  16. // 简单起见,先尝试 navigateTo 跳转,失败后,再尝试 tabBar 跳转
  17. uni.navigateTo({
  18. url: path,
  19. fail(res) {
  20. if (res.errMsg.indexOf('tabbar') > -1) {
  21. uni.switchTab({
  22. url: path,
  23. fail(res) {
  24. console.error(res.errMsg);
  25. },
  26. });
  27. }
  28. else {
  29. console.error(res.errMsg);
  30. }
  31. },
  32. });
  33. });
  34. }
  35. }
  36. // @ts-expect-error
  37. uni.invokePushCallback({
  38. type: 'enabled',
  39. offline: true,
  40. });
  41. Promise.resolve().then(() => {
  42. initPushNotification();
  43. plus.push.setAutoNotification && plus.push.setAutoNotification(false);
  44. });