Code Monkey home page Code Monkey logo

php-huawei-pushkit's Introduction

HUAWEI Push Kit SDK for PHP

Latest Stable Version Total Downloads Latest Unstable Version License

Simple SDK for HUAWEI Push Kit server.

Installation

Use Composer to install the package:

$ composer require megakit/php-huawei-pushkit

Quickstart example

<?php

use MegaKit\Huawei\PushKit\Data\Destination\TokenDestination;
use MegaKit\Huawei\PushKit\Data\Message\Android\AndroidClickAction;
use MegaKit\Huawei\PushKit\DataBuilder\Message\Android\AndroidClickActionBuilder;
use MegaKit\Huawei\PushKit\DataBuilder\Message\Android\AndroidConfigBuilder;
use MegaKit\Huawei\PushKit\DataBuilder\Message\Android\AndroidNotificationBuilder;
use MegaKit\Huawei\PushKit\DataBuilder\Message\MessageBuilder;
use MegaKit\Huawei\PushKit\DataBuilder\Message\NotificationBuilder;
use MegaKit\Huawei\PushKit\Exceptions\HuaweiException;
use MegaKit\Huawei\PushKit\Factory\SenderFactory;
use MegaKit\Huawei\PushKit\HttpConfigBuilder;
use MegaKit\Huawei\PushKit\HuaweiConfigBuilder;

$config = [
    'app_id' => '<APP_ID>',
    'oauth_client_id' => '<OAUTH_CLIENT_ID>',
    'oauth_client_secret' => '<OAUTH_CLIENT_SECRET>',
];

$tokens = [
    '<PUSH_TOKEN_OF_TARGET_USER>',
];

$huaweiConfig = HuaweiConfigBuilder::withDefaults()
    ->setAppId($config['app_id'])
    ->setOAuthCredentials($config['oauth_client_id'], $config['oauth_client_secret'])
    ->build();

$httpConfig = HttpConfigBuilder::withDefaults()->build();

$senderFactory = new SenderFactory();
$sender = $senderFactory->createSenderWithConfig($huaweiConfig, $httpConfig);

$notification = NotificationBuilder::make()
    ->setTitle('Hello')
    ->setBody('World')
    ->build();

$androidNotification = AndroidNotificationBuilder::make()
    ->setClickAction(
        AndroidClickActionBuilder::make()
            ->setType(AndroidClickAction::TYPE_START_APP)
            ->build()
    )
    ->build();

$androidConfig = AndroidConfigBuilder::make()
    ->setNotification($androidNotification)
    ->build();

$message = MessageBuilder::make()
    ->setNotification($notification)
    ->setAndroid($androidConfig)
    ->setData(['hello' => 'world'])
    ->build();

$destination = new TokenDestination($tokens);

try {
    $sender->sendMessage($message, $destination);
    echo 'Message sent successfully' . PHP_EOL;
} catch (HuaweiException $e) {
    echo 'Error sending message: ' . $e->getMessage() . ' (' . $e->getCode() . ')' . PHP_EOL;
}

License

All contents of this package are licensed under the MIT license.

php-huawei-pushkit's People

Contributors

abler98 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.