The purpose of the PushNotifier.js library is to make is super easy to add simple Push notifications to any web application. It uses the jQuery Gritter Growl plugin for the UI and Pusher for realtime push notifications.
The first version of this sample shows examples of server functionality in PHP and Ruby with Sinatra. If you'd like to see the example in other languages please get in touch.
It's as easy as:
-
Include the PusherNotifier.js libraries and the jQuery Gritter files:
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="html5-realtime-push-notifications/lib/gritter/js/jquery.gritter.min.js"></script> <link href="html5-realtime-push-notifications/lib/gritter/css/jquery.gritter.css"rel="stylesheet" type="text/css" /> <script src="//js.pusher.com/2.2/pusher.min.js"></script> <script src="html5-realtime-push-notifications/PusherNotifier.js"></script>
-
Create a
PusherNotifier
instance:$(function() { var pusher = new Pusher('YOUR_APP_KEY'); var channel = pusher.subscribe('my_notifications'); var notifier = new PusherNotifier(channel); });
-
Trigger events on your server and see them instantly appear in your web app:
PHP
$app_key = 'YOUR_APP_KEY'; $app_secret = 'YOUR_APP_SECRET'; $app_id = 'YOUR_APP_ID'; $pusher = new Pusher($app_key, $app_secret, $app_id); $data = array('message' => 'This is an HTML5 Realtime Push Notification!'); $pusher->trigger('my_notifications', 'notification', $data);
Ruby require 'pusher'
Pusher.app_id = 'YOUR_APP_ID'
Pusher.key = 'YOUR_APP_KEY'
Pusher.secret = 'YOUR_APP_SECRET'
data = {'message' => 'This is an HTML5 Realtime Push Notification!'}
Pusher['my_notifications'].trigger('notification', data)
A tutorial explaining how to use the example can be found here: http://pusher.com/tutorials/html5_realtime_push_notifications
http://html5-realtime-push-notifications.pusher.io/
- Rename
examples/php/config.example.php
toconfig.php
and add your Pusher app credentials. - Running on a web server navigate to
examples/index.html
to see a side-by-side page example and click the 'Notify' button to trigger a notification.
- Rename
examples/ruby-sinatra/config_example.rb
toconfig.rb
and add your Pusher app credentials - Update
examples/notify.html
so that theNOTIFY_ENDPOINT
has the value/notify
- In
examples/ruby-sinatra
runbundle install
to install the dependencies defined inexamples/ruby-sinatra/Gemfile
- Start the Sinatra server by running
bundle exec ruby -rubygems notify.rb
- Navigate to http://localhost:4567 (default for sinatra) to see a side-by-side page example and click the 'Notify' button to trigger a notification.