maatify/google-fcm

非官方的 Firebase Admin SDK for PHP,是 maatify.dev FCM 处理器的 PHP 库,由我们团队所知

1.1.1 2023-07-26 23:52 UTC

This package is auto-updated.

Last update: 2024-09-27 12:01:42 UTC


README

Current version Packagist PHP Version Support Monthly Downloads Total Downloads Stars

关于

注意

这个库与 kreait/firebase-php 类似

查看 kreait/firebase-php 文档

GitHub 上查看 kreait/firebase-php

安装

composer require maatify/google-fcm

用法

项目实例

use Maatify\FCM\FcmHandler;

require __DIR__ . '/vendor/autoload.php';

$message = new FcmHandler(__credentials_json_file_location__);

注意:$message 将在项目处理程序中全局使用

消息准备和发送

FCM 通知设置器

// Optional
$message->SetNotification('My Custom Title', 'My Custom Body', __image_url__ = '');

FCM 数据设置器

// Optional
$message->SetDate([
        'key1'=>'value1',
        'key2'=>'value2',
    ]);

向设备令牌发送 FCM

try {
    // $message->sender cannot callable before setting at least one of optional setter
    $result = $message->sender->ToDeviceToken(__device_token__);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);

向多个设备令牌发送 FCM

// $message->sender cannot callable before setting at least one of optional setter
$result = $message->sender->ToMultipleDevicesToken([__device_token1__, __device_token2__]);

print_r($result);

向主题发送 FCM

try {
    // $message->sender cannot callable before setting at least one of optional setter
    $result = $message->sender->ToTopic(__topic__);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);

主题验证

如果您有一组注册令牌,您想检查其有效性或它们是否仍然注册到您的项目中,您可以使用 validateTokens() 方法

主题验证

try {

    $tokens = [__device_token__]; // to validate one token only
    $result = $message->TopicValidation()->ValidateRegistrationTokens($tokens);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);

主题验证

try {

    $tokens = [__device_token1__, __device_token2__]; // to validate many tokens
    $result = $message->TopicValidation()->ValidateRegistrationTokens($tokens);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);

注意

  • valid 包含所有有效且注册到当前 Firebase 项目的令牌
  • unknown 包含所有有效但未注册到当前 Firebase 项目的令牌
  • invalid 包含所有无效(=格式不正确)的令牌

主题管理

您可以使用以下方法将一个或多个设备订阅到一个或多个消息主题

主题实例

$topic_manager = $message->TopicManagement();

注意:对于所有主题管理

  • $registrationTokenOrTokens 可以是字符串(如果只有一个令牌)或数组(如果有多个令牌)
    $registrationTokenOrTokens = __device_token__;
    // or
    $registrationTokenOrTokens = [__device_token1__, __device_token2__];
  • $topic 是字符串形式的设备令牌
    $topic = 'topic-a';
  • $topics 是字符串数组形式的设备令牌
    $topics = ['topic-a', 'topic-b'];

订阅到主题

$result = $topic_manager->SubscribeToTopic($topic, $registrationTokenOrTokens);

print_r($result);

订阅到多个主题

$result = $topic_manager->SubscribeToTopics($topics, registrationTokenOrTokens);

print_r($result);

从主题取消订阅

$result = $topic_manager->UnsubscribeFromTopic($topic, $registrationTokenOrTokens);

print_r($result);

从多个主题取消订阅

$result = $topic_manager->SubscribeToTopics($topics, registrationTokenOrTokens);

print_r($result);

从所有主题取消订阅

$result = $topic_manager->UnsubscribeFromAllTopics(registrationTokenOrTokens);

print_r($result);

结果将返回一个数组,其键是主题名称,值是单个令牌的操作结果。

注意

您可以在单个请求中订阅最多 1,000 台设备。如果您提供的注册令牌数组超过 1,000 个,操作将失败并返回错误。