DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android
  • Send Push Notifications to Users of Another App

Trending

  • Liquid Glass, Material 3, and a Lot of Plumbing
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Is the Data Warehouse Dead? 3 Patterns From Enterprise Architecture That Answer This Question
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Generating push notifications with Pushbullet and Silex

Generating push notifications with Pushbullet and Silex

It's extremely easy to create a service provider and generate push notifications in your mobile app using Pushbullet. Here's how to do it with the Silex PHP framework.

By 
Gonzalo Ayuso user avatar
Gonzalo Ayuso
·
Aug. 25, 15 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.6K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes I need to send push notifications to mobile apps (Android or iOS). It’s not difficult. Maybe it’s a bit of a nightmare the first few times, but when you understand the process it’s straightforward. Over the last few days I discovered a cool service called PushBullet. It allows me to install one client on Android/iOS and one on the desktop, and then send push notifications between them.

Pushbullet also has a good API, and it allows us to automate our push notifications. I’ve played a little bit with the API and my Raspberry Pi home server. It’s really simple to integrate the API with our Silex backend and send push notifications to our registered devices.

I’ve created one small service provider to enclose the API. The idea is to use one Silex application like this

use Silex\Application;
use PushSilex\Silex\Provider\PushbulletServiceProvider;

$app = new Application(['debug' => true]);

$myToken = include(__DIR__ . '/../conf/token.php');

$app->register(new PushbulletServiceProvider($myToken));

$app->get("/", function () {
    return "Usage: GET /note/{title}/{body}";
});

$app->get("/note/{title}/{body}", function (Application $app, $title, $body) {
    return $app->json($app['pushbullet.note']($title, $body));
});

$app->run();


As you can see, we’re using one service provider called PushbulletServiceProvider. This service provides us with pushbullet.note and allows us to send push notifications. We only need to configure our Service Provider with our Pushbullet’s token and that’s all.

<?php
namespace PushSilex\Silex\Provider;
use Silex\ServiceProviderInterface;
use Silex\Application;
class PushbulletServiceProvider implements ServiceProviderInterface
{
    private $accessToken;
    const URI = 'https://api.pushbullet.com/v2/pushes';
    const NOTE = 'note';
    public function __construct($accessToken)
    {
        $this->accessToken = $accessToken;
    }
    public function register(Application $app)
    {
        $app['pushbullet.note'] = $app->protect(function ($title, $body) {
            return $this->push(self::NOTE, $title, $body);
        });
    }
    private function push($type, $title, $body)
    {
        $data = [
            'type'  => $type,
            'title' => $title,
            'body'  => $body,
        ];
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL            => self::URI,
            CURLOPT_HTTPHEADER     => ['Content-Type' => 'application/json'],
            CURLOPT_CUSTOMREQUEST  => 'POST',
            CURLOPT_POSTFIELDS     => $data,
            CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
            CURLOPT_USERPWD        => $this->accessToken . ':',
            CURLOPT_RETURNTRANSFER => true
        ]);
        $out = curl_exec($ch);
        curl_close($ch);

        return json_decode($out);
    }
    public function boot(Application $app)
    {
    }
}


Normally I use Guzzle to handle HTTP clients, but in this example I’ve created a raw curl connection.

You can see the project in my github account.

push

Published at DZone with permission of Gonzalo Ayuso. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android
  • Send Push Notifications to Users of Another App

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook