notificationSetup.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * App 端:通知运行时权限(Android 13+)、iOS 本地通知授权、本地推送点击跳转会话
  3. */
  4. // #ifdef APP-PLUS
  5. import { getToken } from './api'
  6. let pushClickRegistered = false
  7. let iosAuthAsked = false
  8. function parsePushPayload(msg) {
  9. if (!msg) return null
  10. let payload = msg.payload
  11. if (payload == null && msg.Payload != null) payload = msg.Payload
  12. if (typeof payload === 'string') {
  13. try {
  14. payload = JSON.parse(payload)
  15. } catch (e) {
  16. return null
  17. }
  18. }
  19. if (payload && typeof payload === 'object') {
  20. const cid = payload.contactId != null ? payload.contactId : payload.otherUserId
  21. if (cid != null && cid !== '') return String(cid)
  22. }
  23. return null
  24. }
  25. function navigateToChatByContactId(contactId) {
  26. const url =
  27. '/pages/chat/index?otherUserId=' + encodeURIComponent(String(contactId))
  28. uni.navigateTo({
  29. url,
  30. fail: () => {
  31. uni.reLaunch({ url })
  32. }
  33. })
  34. }
  35. function ensureAndroidPostNotifications() {
  36. try {
  37. const Build = plus.android.importClass('android.os.Build')
  38. if (Build.VERSION.SDK_INT < 33) return
  39. const main = plus.android.runtimeMainActivity()
  40. const PackageManager = plus.android.importClass('android.content.pm.PackageManager')
  41. if (
  42. main.checkSelfPermission('android.permission.POST_NOTIFICATIONS') ===
  43. PackageManager.PERMISSION_GRANTED
  44. ) {
  45. return
  46. }
  47. plus.android.requestPermissions(
  48. ['android.permission.POST_NOTIFICATIONS'],
  49. () => {},
  50. (e) => console.warn('[notification] Android POST_NOTIFICATIONS', e && e.message)
  51. )
  52. } catch (e) {
  53. console.warn('[notification] Android permission', e)
  54. }
  55. }
  56. function ensureIOSNotificationAuth() {
  57. if (iosAuthAsked) return
  58. iosAuthAsked = true
  59. try {
  60. const UNUserNotificationCenter = plus.ios.importClass('UNUserNotificationCenter')
  61. const center = UNUserNotificationCenter.currentNotificationCenter()
  62. const opts = 7
  63. center.requestAuthorizationWithOptionsCompletionHandler(opts, function (granted, err) {
  64. if (err) console.warn('[notification] iOS authorization', err)
  65. })
  66. } catch (e) {
  67. console.warn('[notification] iOS permission', e)
  68. iosAuthAsked = false
  69. }
  70. }
  71. function registerPushClick() {
  72. if (pushClickRegistered) return
  73. if (!plus.push || typeof plus.push.addEventListener !== 'function') return
  74. pushClickRegistered = true
  75. plus.push.addEventListener('click', (msg) => {
  76. if (!getToken()) return
  77. const contactId = parsePushPayload(msg)
  78. if (!contactId) return
  79. navigateToChatByContactId(contactId)
  80. })
  81. }
  82. /**
  83. * 在已登录场景调用:申请权限 + 注册点击(幂等)
  84. */
  85. export function setupAppNotifications() {
  86. try {
  87. if (typeof plus === 'undefined' || !plus.push) return
  88. if (plus.os.name === 'Android') ensureAndroidPostNotifications()
  89. else if (plus.os.name === 'iOS') ensureIOSNotificationAuth()
  90. registerPushClick()
  91. } catch (e) {
  92. console.warn('[notification] setup', e)
  93. }
  94. }
  95. // #endif
  96. // #ifndef APP-PLUS
  97. export function setupAppNotifications() {}
  98. // #endif