Code Monkey home page Code Monkey logo

angular-browser-push-notifications's Introduction

Angular browser push notifications

Enable browser push notifications using Angular

Install

 $ bower install angular-browser-push-notifications

Pre-requisites

  • If you are not running on localhost, you must run this on a HTTPS domain verified in Google developer console
  • If you are using https, you must ensure that the certificate is fully trusted
  • You must have a manifest.json file referenced in your HTML as follows <link rel="manifest" href="manifest.json">
  • You need a service worker available at the root of your site. See examples for sample manifest and service workers
  • Refer to this article for more information on Chrome push notifications

How to use

  • Add browserPushNotifications to the list of dependencies of you Angular app
  • Dependency inject the BrowserPushNotifications service
  • Optionally inject the BrowserPushNotificationsStatus value to handle rejection reasons
  • Call BrowserPushNotifications.register() which returns a promise

API

BrowserPushNotifications.register

Returns a promise which resolves with a subscription object looking like this:

{
  "endpoint":"https://android.googleapis.com/gcm/send/APA91bGOYel46xxxxxx_xxxxxxx…",
  "subscriptionId":"APA91bGOYel46xxxxxx_xxxxxxx…"
}

Note that subscriptionId is deprecated in Chrome 45

The promise can reject with any of the statuses in BrowserPushNotificationsStatus

BrowserPushNotifications.getSubscription

Returns a promise which resolves with a subscription id only

BrowserPushNotificationsStatus

Possible reasons for failure:

  • WORKERS_NOT_SUPPORTED: Workers not supported by the browser
  • NOTIFICATIONS_NOT_SUPPORTED: Notifications not supported by the browser
  • PUSH_NOT_SUPPORTED: Push not supported by the browser
  • USER_BLOCKED_NOTIFICATIONS: The user has set notifications to "Disallowed"
  • WORKER_REGISTRATION_FAIL: Worker registration failed, usually related to invalid certificate
  • FAILED_TO_SUBSCRIBE: Subscription failed, could be related to certificate or to your application key

Configuration methods

BrowserPushNotificationsProvider.setWorkerUrl

Set the url for the service worker which will be registered

Sample code

angular.module('notificationsApp', ['browserPushNotifications'])
  .controller('StatusController', function($scope, BrowserPushNotifications){
    $scope.status = 'Pending';
    BrowserPushNotifications.getSubscriptionId().then(function(id) {
      $scope.status = id;
    }, function(err) {
      $scope.status = 'Error ' + err;
    });
  });

Demo

Demo site

angular-browser-push-notifications's People

Contributors

matiboy avatar

Watchers

 avatar  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.