permission-guide.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { ChangeDetectionStrategy, Component, inject, input, output, signal } from '@angular/core';
  2. import { AppNotificationService } from '../../../core/app-notification.service';
  3. import { JPushService } from '../../../core/jpush.service';
  4. /**
  5. * 权限引导弹层(2026-11-15)
  6. *
  7. * ## 为什么需要
  8. *
  9. * 用户在「我的」页里找通知开关这件事,很多人根本不会做。
  10. * 更常见的情况是:用了一周才想起「这个 App 怎么从来不提醒我」,
  11. * 而那时通知权限早就被某个系统弹窗顺手拒掉了。
  12. *
  13. * 所以在**登录成功后**主动问一次,并且**说清楚要它干什么** ——
  14. * 这比一个沉默的系统权限框有效得多。
  15. *
  16. * ## 适用范围
  17. *
  18. * 🔴 **仅移动端**。桌面浏览器没有这些系统权限,也不需要。
  19. * 调用方负责判断(`isNativeApp()`),本组件不做环境判断。
  20. *
  21. * ## 交互
  22. *
  23. * 只问「通知」这一项。不一次性罗列存储/相机/定位:
  24. * 我们目前真的只用到通知,多要一个权限就是在透支用户信任。
  25. * 将来真要加,往 `ITEMS` 里加一项即可,结构已经支持。
  26. */
  27. @Component({
  28. selector: 'app-permission-guide',
  29. changeDetection: ChangeDetectionStrategy.OnPush,
  30. template: `
  31. @if (open()) {
  32. <div class="pg-root" role="dialog" aria-modal="true" aria-label="开启通知">
  33. <div class="pg-mask" (click)="dismiss.emit()"></div>
  34. <section class="pg-card">
  35. <div class="pg-icon"><i class="fa-solid fa-bell" aria-hidden="true"></i></div>
  36. <h2 class="pg-title">开启通知提醒</h2>
  37. <p class="pg-desc">
  38. 任务跑完、容器有新回复时,我们会发一条通知告诉你。
  39. 不开也能正常用,只是要你自己回来查看。
  40. </p>
  41. <ul class="pg-list">
  42. <li>
  43. <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
  44. <span><b>任务完成</b>:容器处理完你的请求时提醒</span>
  45. </li>
  46. <li>
  47. <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
  48. <span><b>新回复</b>:重要消息到了及时知道</span>
  49. </li>
  50. <li>
  51. <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
  52. <span><b>关闭 App 也能收到</b>:切到后台或退出后,消息仍会推送到通知栏</span>
  53. </li>
  54. </ul>
  55. <p class="pg-note">
  56. 开启后将使用推送服务(极光 JPush)投递消息,仅传输「有新消息」这类通知,
  57. 不包含消息内容。系统要求你确认一次授权,之后可在「我的 → 通知」里随时关闭。
  58. </p>
  59. <div class="pg-actions">
  60. <button type="button" class="pg-btn" (click)="dismiss.emit()">以后再说</button>
  61. <button type="button" class="pg-btn pg-btn--primary" [disabled]="busy()" (click)="allow()">
  62. {{ busy() ? '正在开启…' : '开启通知' }}
  63. </button>
  64. </div>
  65. </section>
  66. </div>
  67. }
  68. `,
  69. styles: [
  70. `
  71. .pg-root {
  72. position: fixed;
  73. inset: 0;
  74. z-index: var(--z-modal);
  75. display: grid;
  76. place-items: center;
  77. padding: var(--sp-6);
  78. }
  79. .pg-mask {
  80. position: absolute;
  81. inset: 0;
  82. background: var(--overlay);
  83. }
  84. .pg-card {
  85. position: relative;
  86. width: 100%;
  87. max-width: 340px;
  88. display: flex;
  89. flex-direction: column;
  90. gap: var(--sp-3);
  91. padding: var(--sp-6) var(--sp-5) var(--sp-5);
  92. border-radius: var(--r-lg);
  93. background: var(--surface);
  94. box-shadow: var(--shadow-overlay);
  95. text-align: center;
  96. }
  97. .pg-icon {
  98. width: 56px;
  99. height: 56px;
  100. margin: 0 auto;
  101. display: grid;
  102. place-items: center;
  103. border-radius: 50%;
  104. background: var(--primary-subtle);
  105. color: var(--primary);
  106. font-size: var(--fs-22);
  107. }
  108. .pg-title {
  109. margin: 0;
  110. font-size: var(--fs-17);
  111. color: var(--ink);
  112. }
  113. .pg-desc {
  114. margin: 0;
  115. font-size: var(--fs-13);
  116. line-height: 1.6;
  117. color: var(--muted);
  118. text-align: left;
  119. }
  120. .pg-list {
  121. margin: 0;
  122. padding: var(--sp-3);
  123. list-style: none;
  124. display: flex;
  125. flex-direction: column;
  126. gap: var(--sp-2);
  127. border-radius: var(--r-md);
  128. background: var(--surface-2);
  129. text-align: left;
  130. }
  131. .pg-list li {
  132. display: flex;
  133. gap: var(--sp-2);
  134. align-items: flex-start;
  135. font-size: var(--fs-13);
  136. line-height: 1.5;
  137. color: var(--ink-2);
  138. }
  139. .pg-list i {
  140. color: var(--success);
  141. font-size: var(--fs-12);
  142. margin-top: 3px;
  143. flex: none;
  144. }
  145. .pg-note {
  146. margin: 0;
  147. font-size: var(--fs-12);
  148. line-height: 1.5;
  149. color: var(--faint);
  150. text-align: left;
  151. }
  152. .pg-actions {
  153. display: flex;
  154. gap: var(--sp-2);
  155. margin-top: var(--sp-1);
  156. }
  157. .pg-btn {
  158. flex: 1;
  159. min-height: var(--touch-min);
  160. border: 1px solid var(--edge);
  161. border-radius: var(--r-md);
  162. background: var(--surface);
  163. color: var(--ink);
  164. font: inherit;
  165. font-size: var(--fs-15);
  166. cursor: pointer;
  167. }
  168. .pg-btn--primary {
  169. border-color: var(--primary);
  170. background: var(--primary);
  171. color: var(--primary-content);
  172. }
  173. .pg-btn:disabled {
  174. opacity: 0.6;
  175. cursor: not-allowed;
  176. }
  177. `,
  178. ],
  179. })
  180. export class PermissionGuideComponent {
  181. private readonly notifySvc = inject(AppNotificationService);
  182. private readonly push = inject(JPushService);
  183. readonly open = input(false);
  184. /** 用户关闭(拒绝或开启后) */
  185. readonly dismiss = output<void>();
  186. protected readonly busy = signal(false);
  187. /** 发起系统授权;被拒时引导到设置页 */
  188. protected async allow(): Promise<void> {
  189. if (this.busy()) return;
  190. this.busy.set(true);
  191. try {
  192. /*
  193. * 🔴 顺序很重要:**先启动推送 SDK,再申请系统通知权限**(2026-11-20)。
  194. *
  195. * ## 为什么先 SDK 后权限
  196. *
  197. * 两个是不同层面的东西:
  198. * · 推送 SDK 需要**用户对隐私政策的同意**(极光合规要求)
  199. * · 系统通知权限是 Android 的运行时权限
  200. *
  201. * 用户点了「开启通知」= 同时同意了这两件事。先把 SDK 起来,
  202. * 它拿到 registrationId 后才有意义(否则系统权限开了也没设备标识可绑)。
  203. *
  204. * ## `start(true)` 的 true 是什么
  205. *
  206. * `privacyConsent: true` —— 原生侧强制要求,不传会被拒(`JPUSH_CONSENT_REQUIRED`)。
  207. * 它同时把这个同意**持久化**到 SharedPreferences `fmode-jpush/consent`,
  208. * 下次启动不需要再问。
  209. *
  210. * 推送没启用(`JPUSH_CONFIGURED=false`)时 `start()` 是 no-op,不报错。
  211. */
  212. await this.push.start(true);
  213. const ok = await this.notifySvc.request();
  214. if (ok) return;
  215. /*
  216. * 系统不再弹窗(用户拒绝过一次)→ 带用户去系统设置页。
  217. * 跳到本 App 应用信息页,第一屏就有「通知管理」。
  218. *
  219. * 🔴 跳转后**不立刻 dismiss**(2026-11-15 修正):
  220. * 原来 `finally` 里无条件关掉弹层,用户被送去设置页、
  221. * 开完权限回来,弹层早没了,也**没有任何地方告诉他成功了没有**。
  222. *
  223. * 现在:发起跳转后就关掉(避免回来时压着一个旧弹层),
  224. * 由 `AppNotificationService` 的 `appStateChange` 在回到前台时
  225. * 刷新状态并 toast 提示「通知权限已开启」。反馈落在 toast 上,
  226. * 不依赖这个弹层还活着。
  227. */
  228. await this.notifySvc.openNotificationSettings();
  229. } finally {
  230. this.busy.set(false);
  231. this.dismiss.emit();
  232. }
  233. }
  234. }