Code Monkey home page Code Monkey logo

push-notifications-php's Introduction

Push notifications Library for PHP

Latest License PHP Test codecov


Supported Protocols

Protocol Supported Driver Options
APNs (Token Based) APNs\Token APNs\Token\Option
APNs (Certificate Based) APNs\Certificate APNs\Certificate\Option
APNs (Binary Provider)
FCM (HTTP v1) FCM\V1 FCM\V1\Option
FCM (Legacy JSON) Deprecated FCM\Json FCM\Json\Option
FCM (Legacy Plain Text) Deprecated FCM\PlainText FCM\PlainText\Option
FCM (XMPP)

Installation

composer require sunaoka/push-notifications-php

Basic Usage

For example, in the case of APNs Token Based.

<?php

use Sunaoka\PushNotifications\Drivers\APNs;
use Sunaoka\PushNotifications\Pusher;

$payload = [
    'aps' => [
        'alert' => [
            'title' => 'Game Request',
            'body'  => 'Bob wants to play poker',
        ],
    ],
];

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = '/path/to/key.p8';
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';

$driver = new APNs\Token($options);

$pusher = new Pusher();
$feedback = $pusher->to('Device token')->send($driver);

$result = $feedback->isSuccess('Device token');
if (! $result) {
    echo $feedback->failure('Device token');
    // BadDeviceToken
}

How to specify options

There are two ways to specify the option.

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = '/path/to/key.p8';
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';

or

$options = new APNs\Token\Option([
    'payload' => $payload,
    'authKey' => '/path/to/key.p8',
    'keyId'   => 'ABCDE12345',
    'teamId'  => 'ABCDE12345',
    'topic'   => 'com.example.app',
]);

Multicast message

Specify an array of device tokens in Pusher::to(). Then, you can distribute to multiple devices.

$pusher = new Pusher();
$feedback = $pusher->to([
    'Device token 1',
    'Device token 2',
    'Device token 3',
])->send($driver);

How to switch between the production and development environments (only APNs)

This is specified as an argument when creating an instance of Pusher.

// Development environment (default)
$pusher = new Pusher(false);
// Production environment
$pusher = new Pusher(true);

Feedback

The return value of Pusher::send() is a Feedback object.

With the Feedback object, you can determine whether the notification succeeded or failed.

$pusher = new Pusher();
$feedback = $pusher->to('Device token')->send($driver);

$result = $feedback->isSuccess('Device token');
if ($result) {
    echo $feedback->success('Device token');
    // 01234567-0123-0123-0123-01234567890A
} else {
    echo $feedback->failure('Device token');
    // BadDeviceToken
}

HTTP Request Option

You can specify Guzzle Request Options as a driver option.

$options = new APNs\Token\Option();
$options->httpOptions = [
    'connect_timeout' => 3.14
    'timeout'         => 3.14,
    'debug'           => true,
];

More examples

More examples can be found in the examples directory.

push-notifications-php's People

Contributors

sunaoka avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

push-notifications-php's Issues

Multicast Feedback

Hi, could you please help me to understand how to implement feedback with multicast message?
Thank you

Would this work for SIP/VoIP on Kamailio/Opensips

Hi there.

Apologies for raising a ticket. Was unsure how to get in touch. I would like to implement this with a SIP proxy (Opensips) to send push notifications to APNS + Firebase. Can your project be used in this scenario?

Thanks.

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.