PHP code example of specialist / yii2-fcm-both-api
1. Go to this page and download the library: Download specialist/yii2-fcm-both-api library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
specialist / yii2-fcm-both-api example snippets
return [
//....
'components' => [
'fcm' => [
'class' => 'specialist\fcm\source\components\Fcm',
'apiVersion' => \specialist\fcm\source\requests\StaticRequestFactory::API_V1,
'apiParams' => [
'privateKey' => '/path/to/your/file/privateKeyFile.json',
],
],
]
];
return [
//....
'components' => [
'fcm' => [
'class' => 'specialist\fcm\source\components\Fcm',
'apiVersion' => \specialist\fcm\source\requests\StaticRequestFactory::LEGACY_API,
'apiParams' => [
'serverKey' => 'aef',
'senderId' => 'fwef',
'proxyUrl' => 'https://example.com:4345', // optional
],
],
]
];
/**
* Class WebApplication
* Include only Web application related components here.
*
* @property \specialist\fcm\source\components\Fcm $fcm
*/
class WebApplication extends yii\web\Application
{
}
Yii::$app->fcm
return [
//....
'components' => [
'fcmApiV1' => [
'class' => 'specialist\fcm\source\components\Fcm',
'apiVersion' => \specialist\fcm\source\requests\StaticRequestFactory::API_V1,
'apiParams' => [
'privateKey' => '/path/to/your/file/privateKeyFile.json',
],
],
'fcmLegacyApi' => [
'class' => 'specialist\fcm\source\components\Fcm',
'apiVersion' => \specialist\fcm\source\requests\StaticRequestFactory::LEGACY_API,
'apiParams' => [
'serverKey' => 'aef',
'senderId' => 'fwef',
],
],
]
];
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOPIC, 'some-topic')
->send();
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
$condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)";
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOPIC_CONDITION, $condition)
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setData(['a' => '1', 'b' => 'test'])
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setNotification('Test Title', 'Test Description')
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Test Title', 'Test Description')
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setAndroidConfig([
'ttl' => '3600s',
'priority' => 'normal',
'notification' => [
'title' => 'Android Title',
'body' => 'Android Description.',
'icon' => 'stock_ticker_update',
'color' => '#ff0000',
],
])
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setApnsConfig([
'headers' => [
'apns-priority' => '10',
],
'payload' => [
'aps' => [
'alert' => [
'title' => 'iOS Title',
'body' => 'iOS Description.',
],
'badge' => 42,
],
],
])
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
->setWebPushConfig([
'notification' => [
'title' => 'Web push Title',
'body' => 'Web push Description.',
'icon' => 'https://my-server/icon.png',
],
])
->send();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Test Title', 'Test Description')
->setAndroidConfig([
'ttl' => '3600s',
'priority' => 'normal',
'notification' => [
'title' => 'Android Title',
'body' => 'Andorid Description.',
'icon' => 'push_icon',
'color' => '#ff0000',
],
])
->setApnsConfig([
'headers' => [
'apns-priority' => '10',
],
'payload' => [
'aps' => [
'alert' => [
'title' => 'iOS Title',
'body' => 'iOS Description.',
],
'badge' => 42,
],
],
])
->setWebPushConfig([
'notification' => [
'title' => 'Web push Title',
'body' => 'Web push Description.',
'icon' => 'https://my-server/icon.png',
],
])
->send();
'title' => 'Android Title',
'body' => 'Android Description.'
'title' => 'Test Title',
'body' => 'Test Description.'
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Test Title', 'Test Description')
->send();
if ($result->isResultOk()) {
echo $result->getRawMessageId();
echo $result->getMessageId();
} else {
$tokensToDelete = $result->getTokensToDelete();
$errorDetails = $result->getErrorDetails();
echo $result->getErrorStatusDescription();
}
$result->getRawMessageId();
$result->getMessageId();
$result->getErrorStatusDescription();
$result->getTokensToDelete();
$result->getError();
$result->getErrorStatus();
$result->getErrorCode();
$result->getErrorMessage();
$result->getErrorDetails();
/** @var \specialist\fcm\source\responses\apiV1\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Test Title', 'Test Description')
->validateOnly()
->send();
if ($result->isResultOk()) {
echo $result->getRawMessageId();
} else {
echo $result->getErrorStatusDescription();
}
$result->getErrorStatusDescription()
$result->getMessageId();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Send push-notification to a single token (device)', 'Test description')
->send();
$tokens = [
'your_token_1',
'your_token_2',
'your_token_3',
];
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKENS, $tokens)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Send push-notification to multiple tokens (devices)', 'Test description')
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TopicResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_TOPIC_SENDING)
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOPIC, 'a-topic')
->setNotification('Test title', 'Test description')
->send();
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
$condition = "'a-topic' in topics && ('b-topic' in topics || 'b-topic' in topics)";
/** @var \specialist\fcm\source\responses\legacyApi\TopicResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_TOPIC_SENDING)
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOPIC_CONDITION, $condition)
->setNotification('Test title', 'Test description')
->send();
$notificationKey = 'your_notification_key';
/** @var \specialist\fcm\source\responses\legacyApi\GroupResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_SENDING)
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::GROUP, $notificationKey)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Test title', 'Test description')
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setNotification('Test title', 'Test Description')
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Send push-notification with both \'Notification\' and \'Data\' message type.', 'Test Description')
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = Yii::$app
->fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, 'your_token')
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setAndroidConfig([
'title' => 'Android Title',
'body' => 'Android Description.',
'icon' => 'stock_ticker_update',
'color' => '#ff0000',
])
->setPriority(\specialist\fcm\source\helpers\OptionsHelper::HIGH)
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setApnsConfig([
'title' => 'iOS Title',
'body' => 'iOS Description.',
'title_loc_key' => 'iOS Title loc key.',
'badge' => '42',
'sound' => 'bingbong.aiff',
])
->send();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setWebPushConfig([
'title' => 'Web push Title',
'body' => 'Web push Description.',
'icon' => 'https://my-server/icon.png',
'click_action' => 'click-action',
])
->send();
->setCollapseKey(')
->setPriority()
->setContentAvailable()
->setMutableContent()
->setTimeToLive()
->setRestrictedPackageName()
->validateOnly()
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Test title', 'Test description')
->setAndroidConfig([
'title' => 'Android Title',
'body' => 'Android Description.',
'icon' => 'stock_ticker_update',
'color' => '#ff0000',
])
->setApnsConfig([
'title' => 'iOS Title',
'body' => 'iOS Description.',
'title_loc_key' => 'iOS Title loc key.',
'badge' => '42',
'sound' => 'bingbong.aiff',
])
->setWebPushConfig([
'title' => 'Web push Title',
'body' => 'Web push Description.',
'icon' => 'https://my-server/icon.png',
'click_action' => 'click-action',
])
->setCollapseKey('collapse_key')
->setPriority(\specialist\fcm\source\helpers\OptionsHelper::NORMAL)
->setContentAvailable(true)
->setMutableContent(false)
->setTimeToLive(300)
->setRestrictedPackageName('restricted_package_mame')
->validateOnly(false)
->send();
'title' => 'Web push Title',
'body' => 'Web push Description.',
'icon' => 'https://my-server/icon.png',
'color' => '#ff0000',
'title_loc_key' => 'iOS Title loc key.',
'badge' => '42',
'sound' => 'bingbong.aiff',
'click_action' => 'click-action',
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Test title', 'Test description')
->send();
if ($result->isResultOk()) {
echo 'MessageId '.$result->getMessageId();
echo 'NumberSuccess '.$result->getNumberSuccess();
echo 'NumberFailure '.$result->getNumberFailure();
echo 'NumberModification '.$result->getNumberModification();
} else {
echo 'numberSuccess '.$result->getNumberSuccess();
echo 'numberFailure '.$result->getNumberFailure();
echo 'numberModification '.$result->getNumberModification();
echo 'TokensToDelete '.$result->getTokensToDelete();
echo 'TokensToModify '.$result->getTokensToModify();
echo 'TokensToRetry '.$result->getTokensToRetry();
echo 'RetryAfter '.$result->getRetryAfter();
echo 'TokensWithError '.$result->getTokensWithError();
echo 'ErrorStatusDescription '.$result->getErrorStatusDescription();
}
$result->getMessageId();
$result->getNumberSuccess();
$result->getNumberFailure();
$result->getNumberModification();
$result->getErrorStatusDescription();
$result->getTokensToDelete();
$result->getTokensToModify();
$result->getTokensToRetry();
$result->getRetryAfter();
$result->getTokensWithError();
/** @var \specialist\fcm\source\responses\legacyApi\TopicResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_TOPIC_SENDING)
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOPIC, 'a-topic')
->setNotification('Test title', 'Test description')
->send();
if ($result->isResultOk()) {
echo 'MessageId '.$result->getMessageId();
} else {
echo 'ErrorMessage '.$result->getErrorMessage();
echo 'ErrorStatusDescription '.$result->getErrorStatusDescription();
}
$result->getMessageId();
$result->getErrorStatusDescription();
$result->getErrorMessage();
$notificationKey = 'your_notification_key';
/** @var \specialist\fcm\source\responses\legacyApi\GroupResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_SENDING)
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::GROUP, $notificationKey)
->setData(['a' => '1', 'b' => '2'])
->setNotification('Test title', 'Test description')
->send();
if ($result->isResultOk()) {
echo 'numberSuccess ' . $result->getNumberSuccess();
echo 'numberFailure ' . $result->getNumberFailure();
} else {
echo 'numberSuccess ' . $result->getNumberSuccess();
echo 'numberFailure ' . $result->getNumberFailure();
echo 'tokensFailed ' . print_r($tokensFailed = $result->getTokensFailed());
echo 'ErrorStatusDescription '.$result->getErrorStatusDescription();
}
$result->getNumberSuccess();
$result->getNumberFailure();
$result->getErrorStatusDescription();
$result->getTokensFailed();
/** @var \specialist\fcm\source\responses\legacyApi\TokenResponse $result */
$result = $fcm
->createRequest()
->setTarget(\specialist\fcm\source\builders\legacyApi\MessageOptionsBuilder::TOKEN, $token)
->setData(['a' => '1', 'b' => 'test'])
->setNotification('Validating messages', 'Test Description')
->validateOnly()
->send();
if ($result->isResultOk()) {
echo $result->getMessageId();
} else {
echo $result->getErrorStatusDescription();
}
$result->getErrorStatusDescription()
$result->getMessageId();
$groupName = 'test-group';
$tokens = [
'your_token_1',
'your_token_2',
'your_token_3',
'your_token_4',
'your_token_5',
];
/** @var \specialist\fcm\source\responses\legacyApi\GroupManagementResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_MANAGEMENT)
->createGroup($groupName, $tokens)
->send();
if ($result->isResultOk()) {
echo 'NotificationKey '.$notificationKey = $result->getNotificationKey();
} else {
echo 'getErrorStatusDescription '. $result->getErrorStatusDescription();
}
$result->getErrorStatusDescription()
$groupName = 'test-group';
/** @var \specialist\fcm\source\responses\legacyApi\GroupManagementResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_MANAGEMENT)
->getNotificationKey($groupName)
->sendGET();
if ($result->isResultOk()) {
echo 'NotificationKey '.$notificationKey = $result->getNotificationKey();
echo '<br>';
} else {
echo 'getErrorStatusDescription '. $result->getErrorStatusDescription();
echo '<br>';
}
$result->getErrorStatusDescription()
$groupName = 'test-group';
$tokens = [
'your_token_6',
'your_token_7',
'your_token_8',
'your_token_9',
'your_token_10',
];
/** @var \specialist\fcm\source\responses\legacyApi\GroupManagementResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_MANAGEMENT)
->addToGroup($groupName, $notificationKey, $tokens)
->send();
if ($result->isResultOk()) {
echo 'NotificationKey '.$result->getNotificationKey();
} else {
echo 'getErrorStatusDescription '. $result->getErrorStatusDescription();
}
$result->getErrorStatusDescription()
$groupName = 'test-group';
$tokens = [
'your_token_6',
'your_token_7',
];
/** @var \specialist\fcm\source\responses\legacyApi\GroupManagementResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_GROUP_MANAGEMENT)
->removeFromGroup($groupName, $notificationKey, $tokens)
->send();
if ($result->isResultOk()) {
echo 'NotificationKey '.$result->getNotificationKey();
echo '<br>';
} else {
echo 'getErrorStatusDescription '. $result->getErrorStatusDescription();
echo '<br>';
}
$result->getErrorStatusDescription()
$topic = 'test-topic';
$tokens = [
'your_token_1',
'your_token_2',
'your_token_3',
'your_token_4',
'your_token_5',
];
/** @var \specialist\fcm\source\responses\TopicSubscribeResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_TOPIC_MANAGEMENT)
->subscribeToTopic($topic, $tokens)
->send();
$tokensWithError = $result->getTopicTokensWithError();
$tokensWithError = [
[
'token' => 'your_token_2',
'error' => 'INVALID_ARGUMENT',
],
[
'token' => 'your_token_4',
'error' => 'INVALID_ARGUMENT',
],
[
'token' => 'your_token_5',
'error' => 'INVALID_ARGUMENT',
],
];
$topic = 'test-topic';
$tokens = [
'your_token_1',
'your_token_2',
'your_token_3',
'your_token_4',
'your_token_5',
];
/** @var \specialist\fcm\source\responses\TopicSubscribeResponse $result */
$result = $fcm
->createRequest(\specialist\fcm\source\builders\StaticBuilderFactory::FOR_TOPIC_MANAGEMENT)
->unsubscribeFromTopic($topic, $tokens)
->send();
$tokensWithError = $result->getTopicTokensWithError();
$tokensWithError = [
[
'token' => 'your_token_2',
'error' => 'INVALID_ARGUMENT',
],
[
'token' => 'your_token_4',
'error' => 'INVALID_ARGUMENT',
],
[
'token' => 'your_token_5',
'error' => 'INVALID_ARGUMENT',
],
];
$ php composer.phar