gitspark / fcmhttpv1
FCM HTTP V1 包
dev-master
2024-08-23 14:51 UTC
Requires
- google/apiclient: ^2.12.1
- google/cloud-binary-authorization: ^0.5.0
This package is not auto-updated.
Last update: 2024-09-21 13:49:24 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 gitspark/fcmhttpv1
- 在config/app.php中注册提供者
gitspark\FcmHttpV1\FcmProvider::class,
- 发布配置文件
php artisan vendor:publish --tag=fcmhttpv1 --ansi --force
Laravel PWA
-
请遵循此教程来配置Laravel PWA。
-
在项目的public文件夹中创建一个名为"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 gitspark\FcmHttpV1\FcmTopicHelper; $tokens = ["first token", ... , "last token"]; FcmTopicHelper::subscribeToTopic($tokens, "myTopic");
取消订阅
use gitspark\FcmHttpV1\FcmTopicHelper; $tokens = ["first token", ... , "last token"]; FcmTopicHelper::unsubscribeToTopic($tokens, "myTopic");
列出订阅
use gitspark\FcmHttpV1\FcmTopicHelper; $token = "your awesome device token"; FcmTopicHelper::getTopicsByToken($token);
通知
您可以向特定用户或主题发送通知。
发送到唯一令牌
use gitspark\FcmHttpV1\FcmNotification; $notif = new FcmNotification(); $notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setToken("put device token here")->setClickAction("/news")->send();
发送到主题
use gitspark\FcmHttpV1\FcmNotification; $notif = new FcmNotification(); $notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setTopic("general_topic")->setClickAction("/news")->send();