granam/php-firebase-cloud-messaging 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

granam/php-firebase-cloud-messaging

Composer 安装命令:

composer require granam/php-firebase-cloud-messaging

包简介

PHP API for Firebase Cloud Messaging from Google

README 文档

README

PHP API for Firebase Cloud Messaging from Google.

This library supports Messages/Notifications via HTTP only.

Requires PHP 7.1, if you have to rely on lower version, use original PHP FCM library or some clone of it.

Send message to Device or Topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmTopicTarget;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;
use granam\FirebaseCloudMessaging\AndroidFcmNotification;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->setPriority('high')
    ->setNotification(new AndroidFcmNotification('A message from Foo!', 'Hi Bar, how are you?'))
    ->setData(['photo' => 'https://example.com/photos/Foo.png']);
// you can NOT combine multiple recipient types as topic and device in a single message
$messageForTopic = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_'));
$messageForTopic
    ->setNotification(new AndroidFcmNotification('Foo is looking for you!', 'Here you are!'))
    ->setData(['map' => 'https://example.com/maps/foo']);

$response = $client->send($message);
var_dump($response);
$responseFromTopic = $client->send($messageForTopic);
var_dump($responseFromTopic);

Send silent push notification to a device

App will have to process the notification by its own as system will not show it.

<?php
use granam\FirebaseCloudMessaging\FcmClient;
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->setData([
        'title' => 'A message from Foo!',
        'message' => 'Hi Bar, how are you?',
        'photo' => 'https://example.com/photos/Foo.png'
    ]);
$response = $client->send($message);
var_dump($response);

Send message to multiple Devices

<?php
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;
use granam\FirebaseCloudMessaging\IosFcmNotification;

$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_2_'))
    ->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_3_'))
    ->setPriority('high')
    ->setNotification(new IosFcmNotification('You are wanted', 'We got some issue here, where are you? We need you.'))
    ->setData(['attachment' => 'data:image/gif;base64,FooBar==']);
// ...

Send message to multiple Topics

See Firebase documentation for sending to combinations of multiple topics.

<?php
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\JsFcmNotification;
use granam\FirebaseCloudMessaging\Target\FcmTopicTarget;

$message = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_'));
$message->addTarget(new FcmTopicTarget('_YOUR_TOPIC_2_'))
    ->addTarget(new FcmTopicTarget('_YOUR_TOPIC_3_'))
    ->setPriority('high')
    ->setNotification(new JsFcmNotification('some title', 'some body'))
    ->setData(['key' => 'value'])
    // will send to devices subscribed to topic 1 AND topic 2 or 3
    ->setCondition('%s && (%s || %s)');

Subscribe user to the topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$response = $client->subscribeToTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

Remove user subscription from the topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');

$response = $client->unsubscribeFromTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

Install

composer require granam/php-firebase-cloud-messaging

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固