定制 gtcrais/nativephp-push-notifications-permission-watcher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gtcrais/nativephp-push-notifications-permission-watcher

Composer 安装命令:

composer require gtcrais/nativephp-push-notifications-permission-watcher

包简介

NativePHP Mobile plugin which emits an event when the push notifications permission changes

README 文档

README

NativePHP Mobile plugin which emits an event when the push notifications permission changes.

Installation

composer require gtcrais/nativephp-push-notifications-permission-watcher

Usage

PHP (Livewire/Blade)

use GTCrais\Native\Mobile\PushNotificationsPermissionWatcher\Facades\PushNotificationsPermissionWatcher;

// Request permission (shows native dialog if not yet determined)
PushNotificationsPermissionWatcher::watch();

// Check current permission status without requesting
$status = PushNotificationsPermissionWatcher::checkPermission(); // 'granted', 'denied', or 'not_determined' (iOS only)

JavaScript (Vue/React/Inertia)

import { On } from '#nativephp';
import { PushNotificationsPermissionWatcher, PushNotificationsPermissionEvents } from '@gtcrais/nativephp-push-notifications-permission-watcher';

// Listen for the permission result
On(PushNotificationsPermissionEvents.PushNotificationsPermissionChanged, ({ status }) => {
    console.log(status); // 'granted' or 'denied'
});

// Trigger the permission dialog
await PushNotificationsPermissionWatcher.watch();

Listening for Events

Livewire

use Native\Mobile\Attributes\OnNative;
use GTCrais\Native\Mobile\PushNotificationsPermissionWatcher\Events\PushNotificationsPermissionChanged;

#[OnNative(PushNotificationsPermissionChanged::class)]
public function handlePushNotificationsPermissionChanged(string $status)
{
    // $status is 'granted' or 'denied'
}

Laravel Event Listener

use GTCrais\Native\Mobile\PushNotificationsPermissionWatcher\Events\PushNotificationsPermissionChanged;

Event::listen(PushNotificationsPermissionChanged::class, function (PushNotificationsPermissionChanged $event) {
    // $event->status is 'granted' or 'denied'
});

Full Code Example

BaseLayout.vue

mounted() {
	PushNotificationService.registerTokenGeneratedListener();
	PushNotificationService.registerPushNotificationsPermissionChangeListener();
}

PushNotificationService.js

import { PushNotifications, On, Events } from '#nativephp';
import { PushNotificationsPermissionWatcher, PushNotificationsPermissionEvents } from '@gtcrais/nativephp-push-notifications-permission-watcher';
import { useAuthStore } from "@/stores/auth-store.js";
import axios from "axios";
import { useAppDataStore } from "@/stores/app-data-store.js";

export default class PushNotificationService
{
	static async enroll()
	{
		await PushNotificationsPermissionWatcher.watch();
	}

	static async refreshPermissionStatus()
	{
		const status = await PushNotifications.checkPermission();
		useAuthStore().setPushNotificationsPermissionStatus(status);
	}

	static async getTokenAndStore()
	{
		const token = await PushNotifications.getToken();

		if (token) {
			await this.storeToken(token);
		}
	}

	static async storeToken(token)
	{
		await axios.post('/push-notifications-token', { token })
			.catch((error) => {
			    console.log('[LC]', JSON.stringify(error));
			});
	}

	static async deleteToken()
	{
		await axios.delete('/push-notifications-token');
	}

	static registerPushNotificationsPermissionChangeListener()
	{
		On(PushNotificationsPermissionEvents.PushNotificationsPermissionChanged, async ({ status }) => {
			setTimeout(async () => {
				await this.refreshPermissionStatus();

				if (this.isGranted) {
					// Now that the permission is granted, this will just go and fetch 
                    // the token, then fire the TokenGenerated event which we listen to
					await PushNotifications.enroll();
				}
			}, 200);
		});
	}

	static registerTokenGeneratedListener()
	{
		On(Events.PushNotification.TokenGenerated, async ({ token }) => {
			await this.handleTokenGenerated(token);
		});
	}

	static async handleTokenGenerated(token)
	{
		if (token) {
			await this.storeToken(token);
		}
	}

	static get permissionStatus()
	{
		return useAuthStore().pushNotificationsPermissionStatus;
	}

	static get isNotDetermined()
	{
		return this.permissionStatus === 'not_determined';
	}

	static get isDetermined()
	{
		return !this.isNotDetermined;
	}

	static get isDenied()
	{
		return this.permissionStatus === 'denied';
	}

	static get isGranted()
	{
		return (this.isDetermined && !this.isDenied);
	}
}

auth-store.js

export const useAuthStore = defineStore('auth', {
	state: () => ({
		_pushNotificationsPermissionStatus: null
	}),

	getters: {
		pushNotificationsPermissionStatus: (state) => state._pushNotificationsPermissionStatus,
	},

	actions: {
		setPushNotificationsPermissionStatus(status) {
			this._pushNotificationsPermissionStatus = status;
		}
	}
});

License

MIT

gtcrais/nativephp-push-notifications-permission-watcher 适用场景与选型建议

gtcrais/nativephp-push-notifications-permission-watcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gtcrais/nativephp-push-notifications-permission-watcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gtcrais/nativephp-push-notifications-permission-watcher 我们能提供哪些服务?
定制开发 / 二次开发

基于 gtcrais/nativephp-push-notifications-permission-watcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-13