appy / fcmhttpv1
FCM HTTP V1 包
dev-master
2023-09-01 10:49 UTC
Requires
- google/apiclient: ^2.12.1
- google/cloud-binary-authorization: ^0.5.0
This package is auto-updated.
Last update: 2024-08-30 01:44:57 UTC
README
这是一个 Laravel 包,它允许您轻松使用新的 FCM Http V1 API 发送推送通知。
摘要
安装
如果您的 Firebase 项目已经设置,您可以跳过这部分并转到 用法部分。
安装将分为两步。首先,我们将通过 Firebase 控制台构建和管理 Firebase 项目。然后,我们将了解如何在您的出色 Laravel 项目中实现 Firebase FCM Http V1。
Firebase
-
前往 Firebase 控制台。
-
选择是否启用分析并创建项目。
-
进入应用的项目设置,切换到“服务帐户”选项卡,然后点击“生成新私钥”。它将下载一个包含您的应用凭据的 json 文件。
-
转到项目设置,云消息选项卡,启用云消息 API(点击右侧的三个点,在 Google Cloud 控制台中管理 API,并启用 API)
-
刷新 Firebase 控制台页面,云消息 API 下方将显示一个服务器密钥。(在云消息选项卡中)
Firebase 配置现已完成。
Laravel
- 将下载的 json 文件放在项目的根目录中。(在 Firebase 配置步骤 7 中下载的 JSON)
- 前往 Firebase 控制台 -> 项目设置 -> 通用,并观察 firebaseConfig。
- 为 .env 变量分配值
FCM_API_KEY="<firebase apiKey>" FCM_AUTH_DOMAIN="<firebase authDomain>" FCM_PROJECT_ID="<firebase projectId>" FCM_STORAGE_BUCKET="<firebase storageBucket>" FCM_MESSAGIN_SENDER_ID="<firebase messagingSenderId>" FCM_APP_ID="<firebase appId>" FCM_JSON="<name of the json file downloaded at firebase step 7 install>" FCM_API_SERVER_KEY=<api server key step 8-9 of firebase install>
- 包安装
composer require appy/fcmhttpv1
- 在 config/app.php 中注册提供者
Appy\FcmHttpV1\FcmProvider::class,
- 发布配置文件
php artisan vendor:publish --tag=fcmhttpv1 --ansi --force
Laravel PWA
-
请遵循此 教程 来配置 Laravel PWA。
-
在您的项目公共文件夹中创建一个名为 "firebase-messaging-sw.js" 的文件。
// Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here. Other Firebase libraries // are not available in the service worker. importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js'); // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object firebase.initializeApp({ apiKey: 'api-key', authDomain: 'project-id.firebaseapp.com', databaseURL: 'https://project-id.firebaseio.com', projectId: 'project-id', storageBucket: 'project-id.appspot.com', messagingSenderId: 'sender-id', appId: 'app-id', }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = firebase.messaging();
用法
主题
主题用于将设备令牌分组。它们将允许您直接向用户已注册的主题发送通知。
订阅
要将令牌订阅到主题
use Appy\FcmHttpV1\FcmTopicHelper; $tokens = ["first token", ... , "last token"]; FcmTopicHelper::subscribeToTopic($tokens, "myTopic");
退订
use Appy\FcmHttpV1\FcmTopicHelper; $tokens = ["first token", ... , "last token"]; FcmTopicHelper::unsubscribeToTopic($tokens, "myTopic");
列出订阅
use Appy\FcmHttpV1\FcmTopicHelper; $token = "your awesome device token"; FcmTopicHelper::getTopicsByToken($token);
通知
您可以向特定用户或主题发送通知。
发送到唯一令牌
use Appy\FcmHttpV1\FcmNotification; $notif = new FcmNotification(); $notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setToken("put device token here")->setClickAction("/news")->send();
发送到主题
use Appy\FcmHttpV1\FcmNotification; $notif = new FcmNotification(); $notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setTopic("general_topic")->setClickAction("/news")->send();