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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Push-Based Systems Fail at Scale — and How Hybrid Fan-Out Fixes It
  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Smarter GitHub Copilot Prompts for Java Microservices: From APIs to Production Tests
  • Seven AI Adoption Failures and How to Overcome Them

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 25, 15 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Why Push-Based Systems Fail at Scale — and How Hybrid Fan-Out Fixes It
  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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