uni-push-v1.plus.es.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. });