| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import { ChangeDetectionStrategy, Component, inject, input, output, signal } from '@angular/core';
- import { AppNotificationService } from '../../../core/app-notification.service';
- import { JPushService } from '../../../core/jpush.service';
- /**
- * 权限引导弹层(2026-11-15)
- *
- * ## 为什么需要
- *
- * 用户在「我的」页里找通知开关这件事,很多人根本不会做。
- * 更常见的情况是:用了一周才想起「这个 App 怎么从来不提醒我」,
- * 而那时通知权限早就被某个系统弹窗顺手拒掉了。
- *
- * 所以在**登录成功后**主动问一次,并且**说清楚要它干什么** ——
- * 这比一个沉默的系统权限框有效得多。
- *
- * ## 适用范围
- *
- * 🔴 **仅移动端**。桌面浏览器没有这些系统权限,也不需要。
- * 调用方负责判断(`isNativeApp()`),本组件不做环境判断。
- *
- * ## 交互
- *
- * 只问「通知」这一项。不一次性罗列存储/相机/定位:
- * 我们目前真的只用到通知,多要一个权限就是在透支用户信任。
- * 将来真要加,往 `ITEMS` 里加一项即可,结构已经支持。
- */
- @Component({
- selector: 'app-permission-guide',
- changeDetection: ChangeDetectionStrategy.OnPush,
- template: `
- @if (open()) {
- <div class="pg-root" role="dialog" aria-modal="true" aria-label="开启通知">
- <div class="pg-mask" (click)="dismiss.emit()"></div>
- <section class="pg-card">
- <div class="pg-icon"><i class="fa-solid fa-bell" aria-hidden="true"></i></div>
- <h2 class="pg-title">开启通知提醒</h2>
- <p class="pg-desc">
- 任务跑完、容器有新回复时,我们会发一条通知告诉你。
- 不开也能正常用,只是要你自己回来查看。
- </p>
- <ul class="pg-list">
- <li>
- <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
- <span><b>任务完成</b>:容器处理完你的请求时提醒</span>
- </li>
- <li>
- <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
- <span><b>新回复</b>:重要消息到了及时知道</span>
- </li>
- <li>
- <i class="fa-solid fa-circle-check" aria-hidden="true"></i>
- <span><b>关闭 App 也能收到</b>:切到后台或退出后,消息仍会推送到通知栏</span>
- </li>
- </ul>
- <p class="pg-note">
- 开启后将使用推送服务(极光 JPush)投递消息,仅传输「有新消息」这类通知,
- 不包含消息内容。系统要求你确认一次授权,之后可在「我的 → 通知」里随时关闭。
- </p>
- <div class="pg-actions">
- <button type="button" class="pg-btn" (click)="dismiss.emit()">以后再说</button>
- <button type="button" class="pg-btn pg-btn--primary" [disabled]="busy()" (click)="allow()">
- {{ busy() ? '正在开启…' : '开启通知' }}
- </button>
- </div>
- </section>
- </div>
- }
- `,
- styles: [
- `
- .pg-root {
- position: fixed;
- inset: 0;
- z-index: var(--z-modal);
- display: grid;
- place-items: center;
- padding: var(--sp-6);
- }
- .pg-mask {
- position: absolute;
- inset: 0;
- background: var(--overlay);
- }
- .pg-card {
- position: relative;
- width: 100%;
- max-width: 340px;
- display: flex;
- flex-direction: column;
- gap: var(--sp-3);
- padding: var(--sp-6) var(--sp-5) var(--sp-5);
- border-radius: var(--r-lg);
- background: var(--surface);
- box-shadow: var(--shadow-overlay);
- text-align: center;
- }
- .pg-icon {
- width: 56px;
- height: 56px;
- margin: 0 auto;
- display: grid;
- place-items: center;
- border-radius: 50%;
- background: var(--primary-subtle);
- color: var(--primary);
- font-size: var(--fs-22);
- }
- .pg-title {
- margin: 0;
- font-size: var(--fs-17);
- color: var(--ink);
- }
- .pg-desc {
- margin: 0;
- font-size: var(--fs-13);
- line-height: 1.6;
- color: var(--muted);
- text-align: left;
- }
- .pg-list {
- margin: 0;
- padding: var(--sp-3);
- list-style: none;
- display: flex;
- flex-direction: column;
- gap: var(--sp-2);
- border-radius: var(--r-md);
- background: var(--surface-2);
- text-align: left;
- }
- .pg-list li {
- display: flex;
- gap: var(--sp-2);
- align-items: flex-start;
- font-size: var(--fs-13);
- line-height: 1.5;
- color: var(--ink-2);
- }
- .pg-list i {
- color: var(--success);
- font-size: var(--fs-12);
- margin-top: 3px;
- flex: none;
- }
- .pg-note {
- margin: 0;
- font-size: var(--fs-12);
- line-height: 1.5;
- color: var(--faint);
- text-align: left;
- }
- .pg-actions {
- display: flex;
- gap: var(--sp-2);
- margin-top: var(--sp-1);
- }
- .pg-btn {
- flex: 1;
- min-height: var(--touch-min);
- border: 1px solid var(--edge);
- border-radius: var(--r-md);
- background: var(--surface);
- color: var(--ink);
- font: inherit;
- font-size: var(--fs-15);
- cursor: pointer;
- }
- .pg-btn--primary {
- border-color: var(--primary);
- background: var(--primary);
- color: var(--primary-content);
- }
- .pg-btn:disabled {
- opacity: 0.6;
- cursor: not-allowed;
- }
- `,
- ],
- })
- export class PermissionGuideComponent {
- private readonly notifySvc = inject(AppNotificationService);
- private readonly push = inject(JPushService);
- readonly open = input(false);
- /** 用户关闭(拒绝或开启后) */
- readonly dismiss = output<void>();
- protected readonly busy = signal(false);
- /** 发起系统授权;被拒时引导到设置页 */
- protected async allow(): Promise<void> {
- if (this.busy()) return;
- this.busy.set(true);
- try {
- /*
- * 🔴 顺序很重要:**先启动推送 SDK,再申请系统通知权限**(2026-11-20)。
- *
- * ## 为什么先 SDK 后权限
- *
- * 两个是不同层面的东西:
- * · 推送 SDK 需要**用户对隐私政策的同意**(极光合规要求)
- * · 系统通知权限是 Android 的运行时权限
- *
- * 用户点了「开启通知」= 同时同意了这两件事。先把 SDK 起来,
- * 它拿到 registrationId 后才有意义(否则系统权限开了也没设备标识可绑)。
- *
- * ## `start(true)` 的 true 是什么
- *
- * `privacyConsent: true` —— 原生侧强制要求,不传会被拒(`JPUSH_CONSENT_REQUIRED`)。
- * 它同时把这个同意**持久化**到 SharedPreferences `fmode-jpush/consent`,
- * 下次启动不需要再问。
- *
- * 推送没启用(`JPUSH_CONFIGURED=false`)时 `start()` 是 no-op,不报错。
- */
- await this.push.start(true);
- const ok = await this.notifySvc.request();
- if (ok) return;
- /*
- * 系统不再弹窗(用户拒绝过一次)→ 带用户去系统设置页。
- * 跳到本 App 应用信息页,第一屏就有「通知管理」。
- *
- * 🔴 跳转后**不立刻 dismiss**(2026-11-15 修正):
- * 原来 `finally` 里无条件关掉弹层,用户被送去设置页、
- * 开完权限回来,弹层早没了,也**没有任何地方告诉他成功了没有**。
- *
- * 现在:发起跳转后就关掉(避免回来时压着一个旧弹层),
- * 由 `AppNotificationService` 的 `appStateChange` 在回到前台时
- * 刷新状态并 toast 提示「通知权限已开启」。反馈落在 toast 上,
- * 不依赖这个弹层还活着。
- */
- await this.notifySvc.openNotificationSettings();
- } finally {
- this.busy.set(false);
- this.dismiss.emit();
- }
- }
- }
|